用AI赚第一桶💰低成本搭建一套AI赚钱工具,源码可二开。 广告
# HttpClientPool Http请求客户端连接池。 [TOC] 使用方式简单,步骤按照下面的来。 ## 步骤1 首先我们打开AppServer,添加连接池的声明。 ```php public function initAsynPools() { parent::initAsynPools(); $this->addAsynPool('GetIPAddress',new HttpClientPool($this->config,'http://int.dpool.sina.com.cn')); $this->addAsynPool('WeiXinAPI',new HttpClientPool($this->config,'https://api.weixin.qq.com')); } ``` 我们通过继承initAsynPools的方法为我们的框架添加了2个http连接池。 函数原型 ``` function addAsynPool($name, AsynPool $pool) ``` $name为改连接池的名称,$pool为连接池的实例 ``` public function __construct($config, $baseUrl) ``` HttpClientPool的构造函数,$config传框架的Config实例,$baseUrl请注意如果你要访问https://api.weixin.qq.com/1/2/34/5/6/abc类似这样的网址,那么它的baseUrl为https://api.weixin.qq.com。 ## 步骤2 我们打开我们需要进行访问的代码,以下我们假设在controller中进行访问。 ```php /** * @var HttpClientPool */ protected $GetIPAddressHttpClient; public function initialization($controller_name, $method_name) { parent::initialization($controller_name, $method_name); $this->GetIPAddressHttpClient = get_instance()->getAsynPool('GetIPAddress'); } ``` 我们在initialization初始化函数中获得这个HttpClientPool。 ```php public function http_ip_test() { $ip = $this->http_input->server('remote_addr'); $response = yield $this->GetIPAddressHttpClient->httpClient ->setQuery(['format' => 'json', 'ip' => $ip]) ->coroutineExecute('/iplookup/iplookup.php'); } ``` 接下来我们就可以在代码中书写我们需要访问的api了。