在写后台代码时,避免不了需要与其他第三方接口交互,如向服务号下发模板消息,有时可能需要下发超过 10 万条。这时不得不考虑使用异步和「多线程」的网络请求。
今天向 php 工程师们推荐一个 guzzle 插件。
guzzle
guzzle 是一个 php 的 http 客户端,用来轻而易举地发送请求,并集成到我们的 web 服务上。
接口简单:构建查询语句、post 请求、分流上传下载大文件、使用 http cookies、上传 json 数据等等。
发送同步或异步的请求均使用相同的接口。
使用 psr-7 接口来请求、响应、分流,允许你使用其他兼容的 psr-7 类库与 guzzle 共同开发。
抽象了底层的 http 传输,允许你改变环境以及其他的代码,如:对 curl与 php 的流或 socket 并非重度依赖,非阻塞事件循环。
中间件系统允许你创建构成客户端行为。
安装 guzzle
本文结合 laravel 项目介绍 guzzle 基本使用,所以使用 composer 来安装 guzzle 再适合不过了,而且 guzzle 官网也推荐使用 composer 来安装。
composer require guzzlehttp/guzzle:~6.0// 或者php composer.phar require guzzlehttp/guzzle:~6.0
发送简单的 post 请求
访问第三方接口,基本上都是 post 请求为主。如你想做一个简单的智能聊天工具,这时候可以借助图灵机器人 api,发送一个 post 请求获取自动回答内容,直接上代码:
<?phpnamespace app\http\controllers;use guzzlehttp\client;use illuminate\http\request;class guzzleusecontroller extends controller { public function tuling(request $request) { $params = [ 'key' => '*****', 'userid' => 'yemeishu' ]; $params['info'] = $request->input('info', '你好吗'); $client = new client(); $options = json_encode($params, json_unescaped_unicode); $data = [ 'body' => $options, 'headers' => ['content-type' => 'application/json'] ]; // 发送 post 请求 $response = $client->post('http://www.tuling123.com/openapi/api', $data); $callback = json_decode($response->getbody()->getcontents()); return $this->output_json('200', '测试图灵机器人返回结果', $callback); }}
guzzle client->post 函数还是很简单的,只需要访问的接口,和请求的参数,参数中主要包含:body、headers、query等,具体可参考
http://guzzle-cn.readthedocs.io/zh_cn/latest/quickstart.html#id8
测试下:
注:图灵机器人还是很智能的,根据相同的 userid 能够识别上下文,做到智能聊天的。
发送异步的 post 请求
在 php 开发中主要是「面向过程」式的开发方式,但请求第三方接口时,有时候并不需要等待第三方接口返回结果才继续执行。如用户购买成功时,我们需要向短信接口,发送一个 post 请求,由短信平台发送一条短信给用户,告知用户支付成功了,因为这类「提醒消息」属于「额外的附加功能」,并不需要在用户支付时「知道」有没有发送提醒成功。
这时候可以使用 guzzle 的异步请求功能,直接看代码:
public function sms(request $request) { $code = $request->input('code'); $client = new client(); $sid = '9815b4a2bb6d5******8bdb1828644f2'; $time = '20171029173312'; $token = 'af8728c8bc*******12019c680df4b11c'; $sig = strtoupper(md5($sid.$token.$time)); $auth = trim(base64_encode($sid . ":" . $time)); $params = ['templatesms' => [ 'appid' => '12b43**********0091c73c0ab', 'param' => "coding01,$code,30", 'templateid' => '3***3', 'to' => '17689974321' ] ]; $options = json_encode($params, json_unescaped_unicode); $data = [ 'query' => [ 'sig' => $sig ], 'body' => $options, 'headers' => [ 'content-type' => 'application/json', 'authorization' => $auth ] ]; // 发送 post 请求 $promise = $client->requestasync('post', 'https://api.ucpaas.com/2014-06-30/accounts/9815b4a2bb6d5******8bdb1828644f2/messages/templatesms', $data); $promise->then( function (responseinterface $res) { log::info('---'); log::info($res->getstatuscode() . "\n"); log::info($res->getbody()->getcontents() . "\n"); }, function (requestexception $e) { log::info('-__-'); log::info($e->getmessage() . "\n"); } ); $promise->wait(); return $this->output_json('200', '测试短信 api', []);}
先返回接口数据:
然后再输出 log:
[2017-10-29 09:53:14] local.info: --- [2017-10-29 09:53:14] local.info: 200 [2017-10-29 09:53:14] local.info: {"resp":{"respcode":"000000","templatesms":{"createdate":"20171029175314","smsid":"24a93f323c9*****8608568"}}}
最后收到短信信息:
发送多线程异步 post 请求
「发送多线程异步 post 请求」在很多场合中使用到的,如:双十一快到了,可以做一些回馈老用户的活动,这是就需要批量的向老用户推送一条模板消息,告诉用户参与哪些活动的。这时候就需要用到多线程异步请求微信公众号接口。
直接上代码:
public function send($templateid, $openid, $url, $data) { $client = $this->bnotice->gethttp()->getclient(); $requests = function ($open_ids) use ($templateid, $url, $data) { foreach($open_ids as $v){ try { yield $this->bnotice ->template($templateid) ->to($v) ->url($url) ->data($data) ->request(); } catch(exception $e) { log::error('sendtemplate:'.$e->getmessage()); } } }; $pool = new pool($client, $requests($openid), [ 'concurrency' => 16, 'fulfilled' => function ($response, $index) { }, 'rejected' => function ($reason, $index) { }, ]); $promise = $pool->promise(); $promise->wait(); }
其中 request 方法:
public function request($data = []) { $params = array_merge([ 'touser' => '', 'template_id' => '', 'url' => '', 'topcolor' => '', 'miniprogram' => [], 'data' => [], ], $data); $required = ['touser', 'template_id']; foreach ($params as $key => $value) { if (in_array($key, $required, true) && empty($value) && empty($this->message[$key])) { throw new invalidargumentexception("attribute '$key' can not be empty!"); } $params[$key] = empty($value) ? $this->message[$key] : $value; } $params['data'] = $this->formatdata($params['data']); $this->message = $this->messagebackup; $options = json_encode ( $params, json_unescaped_unicode); $data = [ 'query' => [ 'access_token' => $this->getaccesstoken()->gettoken() ], 'body' => $options, 'headers' => ['content-type' => 'application/json'] ]; return function() use ($data) { return $this->gethttp()->getclient()->requestasync('post', $this::api_send_notice, $data); }; }
guzzle 多线程异步请求原型函数,使用 guzzlehttp\pool 对象
use guzzlehttp\pool;use guzzlehttp\client;use guzzlehttp\psr7\request;$client = new client();$requests = function ($total) { $uri = 'http://127.0.0.1:8126/guzzle-server/perf'; for ($i = 0; $i < $total; $i++) { yield new request('get', $uri); }};$pool = new pool($client, $requests(100), [ 'concurrency' => 5, 'fulfilled' => function ($response, $index) { // this is delivered each successful response }, 'rejected' => function ($reason, $index) { // this is delivered each failed request },]);// initiate the transfers and create a promise$promise = $pool->promise();// force the pool of requests to complete.$promise->wait();
总结
有了 guzzle,极大方便了我们并发异步请求第三方接口。如果时间允许,我们可以看看 guzzle 源代码,看看是如何实现的。
推荐教程:《php教程》
以上就是php网络请求插件guzzle使用的详细内容。