最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

PHP网络请求插件Guzzle使用

来源:动视网 责编:小采 时间:2020-11-03 13:37:42
文档

PHP网络请求插件Guzzle使用

PHP网络请求插件Guzzle使用:在写后台代码时,避免不了需要与其他第三方接口交互,如向服务号下发模板消息,有时可能需要下发超过 10 万条。这时不得不考虑使用异步和「多线程」的网络请求。今天向 PHP 工程师们推荐一个 Guzzle 插件。GuzzleGuzzle 是一个 PHP 的 HTTP 客户端
推荐度:
导读PHP网络请求插件Guzzle使用:在写后台代码时,避免不了需要与其他第三方接口交互,如向服务号下发模板消息,有时可能需要下发超过 10 万条。这时不得不考虑使用异步和「多线程」的网络请求。今天向 PHP 工程师们推荐一个 Guzzle 插件。GuzzleGuzzle 是一个 PHP 的 HTTP 客户端


发送异步的 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() . "
");
 Log::info($res->getBody()->getContents() . "
");
 },
 function (RequestException $e) {
 Log::info('-__-');
 Log::info($e->getMessage() . "
");
 }
 );
 $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 多线程异步请求原型函数,使用 GuzzleHttpPool 对象

use GuzzleHttpPool;use GuzzleHttpClient;use GuzzleHttpPsr7Request;$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使用

PHP网络请求插件Guzzle使用:在写后台代码时,避免不了需要与其他第三方接口交互,如向服务号下发模板消息,有时可能需要下发超过 10 万条。这时不得不考虑使用异步和「多线程」的网络请求。今天向 PHP 工程师们推荐一个 Guzzle 插件。GuzzleGuzzle 是一个 PHP 的 HTTP 客户端
推荐度:
标签: 使用 请求 插件
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top