在寫後臺代碼時,避免不了須要與其餘第三方接口交互,如向服務號下發模板消息,有時可能須要下發超過 10 萬條。這時不得不考慮使用異步和「多線程」的網絡請求。php
今天向 PHP 工程師們推薦一個 Guzzle 插件。html
Guzzle 是一個 PHP 的 HTTP 客戶端,用來垂手可得地發送請求,並集成到咱們的 WEB 服務上。laravel
- 接口簡單:構建查詢語句、POST 請求、分流上傳下載大文件、使用 HTTP cookies、上傳 JSON 數據等等。
- 發送同步或異步的請求均使用相同的接口。
- 使用 PSR-7 接口來請求、響應、分流,容許你使用其餘兼容的 PSR-7 類庫與 Guzzle 共同開發。
- 抽象了底層的 HTTP 傳輸,容許你改變環境以及其餘的代碼,如:對 cURL與 PHP 的流或 socket 並不是重度依賴,非阻塞事件循環。
- 中間件系統容許你建立構成客戶端行爲。
摘自 Guzzle 官網介紹:
http://guzzle-cn.readthedocs.io/zh_CN/latest/index.htmljson
本文結合 Laravel 項目介紹 Guzzle 基本使用,因此使用 composer 來安裝 Guzzle 再適合不過了,並且 Guzzle 官網也推薦使用 composer 來安裝。segmentfault
composer require guzzlehttp/guzzle:~6.0 // 或者 php composer.phar require guzzlehttp/guzzle:~6.0
如何安裝 Composer,能夠看看我以前的文章
https://d.laravel-china.org/docs/5.5/installationwindows
訪問第三方接口,基本上都是 POST 請求爲主。如你想作一個簡單的智能聊天工具,這時候能夠藉助圖靈機器人 API,發送一個 POST 請求獲取自動回答內容,直接上代碼:api
<?php namespace 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等,具體可參考promise
http://guzzle-cn.readthedocs.io/zh_CN/latest/quickstart.html#id8bash
測試下:微信
注:圖靈機器人仍是很智能的,根據相同的 userid 可以識別上下文,作到智能聊天的。
在 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 請求」在不少場合中使用到的,如:雙十一快到了,能夠作一些回饋老用戶的活動,這是就須要批量的向老用戶推送一條模板消息,告訴用戶參與哪些活動的。這時候就須要用到多線程異步請求微信公衆號接口。
直接上代碼:
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 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 源代碼,看看是如何實現的。
1.
在 windows 環境下,解決[GuzzleHttpExceptionRequestException] cURL error 60: SSL certificate problem: unable to get local issuer certific ate
訪問這個網址 https://curl.haxx.se/ca/cacer... 下載文件
而後修改 php.ini curl.cainfo = "D:cacert.pem" cacert.pem文件 隨便放在哪,沒限制。
2.
須要瞭解 Guzzle 更多資料
3.
其它 Guzzle 使用文章
- Laravel中用GuzzleHttp:http://blog.csdn.net/xhanfei/article/details/51908629?locationNum=14&fps=1
- PHP HTTP客戶端-Guzzle原理解析:https://segmentfault.com/p/1210000010203531/read
「完」
coding01 期待您繼續關注
也很感謝您能看到這了