Skip to content

Latest commit

 

History

History
29 lines (25 loc) · 1.07 KB

5.6.1 - swoole_http_client->__construct.md

File metadata and controls

29 lines (25 loc) · 1.07 KB

swoole_http_client->__construct

构造方法,函数原型:

swoole_http_client->__construct(string $host, int port, bool $ssl = false);
  • $host 目标主机的IP地址,$host如果为域名底层需要进行一次DNS查询,这是阻塞IO,请使用Swoole\Async::dnsLookup实现异步非阻塞
  • $port 目标端口,Http一般为80端口,Https一般为443端口
  • $ssl 是否开启TLS/SSL隧道加密,https网站必须设置为true
  • 1.9.15/2.0.9或更高版本增加了超时机制,默认超时时间为500ms,如果你需要请求外网URL请修改timeout为更大的数值

$ssl需要依赖openssl,必须在编译swoole时启用--enable-openssl

使用示例

$cli = new swoole_http_client('www.baidu.com', 80);
$cli->setHeaders([
	'Host' => $domainName,
	"User-Agent" => 'Chrome/49.0.2587.3',
	'Accept' => 'text/html,application/xhtml+xml,application/xml',
	'Accept-Encoding' => 'gzip',
]);
$cli->get('/index.html', function ($cli) {
	echo "Length: " . strlen($cli->body) . "\n";
	echo $cli->body;
});