Swoole的异步客户端给我们提供了异步发送请求,接受数据的功能。这里使用异步客户端请求API数据。
上代码:
class MyClient
{
private $cli;
private $ip;
private $port;
public function __construct($ip, $port, $href)
{
$this->ip = $ip;//请求地址
$this->port = $port;//端口号
Swoole\Async::dnsLookup($this->ip, function ($domainName, $ip) use ($href){
$this->cli = new swoole_http_client($ip, $this->port);//异步非阻塞
$this->cli->setHeaders([
'Host' => $domainName,
"User-Agent" => 'Chrome/49.0.2587.3',
'Accept-Encoding' => 'gzip',
]);
$this->cli->get($href, function () {
//var_dump(json_decode($this->cli->body));
echo $this->ip."\n";
});
});
}
public function onConnect()
{
echo "success:".$this->ip."\n";
}
public function onReceive(swoole_client $cli, $data)
{
echo "Receive:".$this