久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      PHP網(wǎng)絡(luò)請求插件Guzzle使用

      在寫后臺代碼時,避免不了需要與其他第三方接口交互,如向服務(wù)號下發(fā)模板消息,有時可能需要下發(fā)超過 10 萬條。這時不得不考慮使用異步和「多線程」的網(wǎng)絡(luò)請求。

      今天向 PHP 工程師們推薦一個 Guzzle 插件。

      Guzzle

      Guzzle 是一個 PHP 的 HTTP 客戶端,用來輕而易舉地發(fā)送請求,并集成到我們的 WEB 服務(wù)上。

      接口簡單:構(gòu)建查詢語句、POST 請求、分流上傳下載大文件、使用 HTTP cookies、上傳 JSON 數(shù)據(jù)等等。

      發(fā)送同步或異步的請求均使用相同的接口。

      使用 PSR-7 接口來請求、響應(yīng)、分流,允許你使用其他兼容的 PSR-7 類庫與 Guzzle 共同開發(fā)。

      抽象了底層的 HTTP 傳輸,允許你改變環(huán)境以及其他的代碼,如:對 cURL與 PHP 的流或 socket 并非重度依賴,非阻塞事件循環(huán)。

      中間件系統(tǒng)允許你創(chuàng)建構(gòu)成客戶端行為。

      安裝 Guzzle

      本文結(jié)合 Laravel 項目介紹 Guzzle 基本使用,所以使用 composer 來安裝 Guzzle 再適合不過了,而且 Guzzle 官網(wǎng)也推薦使用 composer 來安裝。

      composer require guzzlehttp/guzzle:~6.0 // 或者 php composer.phar require guzzlehttp/guzzle:~6.0

      發(fā)送簡單的 POST 請求

      訪問第三方接口,基本上都是 POST 請求為主。如你想做一個簡單的智能聊天工具,這時候可以借助圖靈機(jī)器人 API,發(fā)送一個 POST 請求獲取自動回答內(nèi)容,直接上代碼:

      <?php namespace AppHttpControllers; use GuzzleHttpClient; use IlluminateHttpRequest; 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']         ];         // 發(fā)送 post 請求         $response = $client->post('http://www.tuling123.com/openapi/api', $data);         $callback = json_decode($response->getBody()->getContents());         return $this->output_json('200', '測試圖靈機(jī)器人返回結(jié)果', $callback);     } }

      Guzzle client->post 函數(shù)還是很簡單的,只需要訪問的接口,和請求的參數(shù),參數(shù)中主要包含:body、headers、query等,具體可參考

      http://guzzle-cn.readthedocs.io/zh_CN/latest/quickstart.html#id8

      測試下:

      PHP網(wǎng)絡(luò)請求插件Guzzle使用

      PHP網(wǎng)絡(luò)請求插件Guzzle使用

      注:圖靈機(jī)器人還是很智能的,根據(jù)相同的 userid 能夠識別上下文,做到智能聊天的。

      發(fā)送異步的 POST 請求

      在 PHP 開發(fā)中主要是「面向過程」式的開發(fā)方式,但請求第三方接口時,有時候并不需要等待第三方接口返回結(jié)果才繼續(xù)執(zhí)行。如用戶購買成功時,我們需要向短信接口,發(fā)送一個 post 請求,由短信平臺發(fā)送一條短信給用戶,告知用戶支付成功了,因為這類「提醒消息」屬于「額外的附加功能」,并不需要在用戶支付時「知道」有沒有發(fā)送提醒成功。

      這時候可以使用 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         ]     ];      // 發(fā)送 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', []); }

      先返回接口數(shù)據(jù):

      PHP網(wǎng)絡(luò)請求插件Guzzle使用

      然后再輸出 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"}}}

      最后收到短信信息:

      PHP網(wǎng)絡(luò)請求插件Guzzle使用

      發(fā)送多線程異步 POST 請求

      「發(fā)送多線程異步 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 多線程異步請求原型函數(shù),使用 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();

      總結(jié)

      有了 Guzzle,極大方便了我們并發(fā)異步請求第三方接口。如果時間允許,我們可以看看 Guzzle 源代碼,看看是如何實現(xiàn)的。

      推薦教程:《PHP教程》

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號