在寫后臺代碼時分唾,避免不了需要與其他第三方接口交互校套,如向服務(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 官網(wǎng)介紹:
http://guzzle-cn.readthedocs.io/zh_CN/latest/index.html
安裝 Guzzle
本文結(jié)合 Laravel 項目介紹 Guzzle 基本使用,所以使用 composer 來安裝 Guzzle 再適合不過了婉弹,而且 Guzzle 官網(wǎng)也推薦使用 composer 來安裝睬魂。
composer require guzzlehttp/guzzle:~6.0
// 或者
php composer.phar require guzzlehttp/guzzle:~6.0
如何安裝 Composer,可以看看我之前的文章
https://d.laravel-china.org/docs/5.5/installation
發(fā)送簡單的 POST 請求
訪問第三方接口镀赌,基本上都是 POST 請求為主氯哮。如你想做一個簡單的智能聊天工具,這時候可以借助圖靈機器人 API商佛,發(fā)送一個 POST 請求獲取自動回答內(nèi)容喉钢,直接上代碼:
<?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']
];
// 發(fā)送 post 請求
$response = $client->post('http://www.tuling123.com/openapi/api', $data);
$callback = json_decode($response->getBody()->getContents());
return $this->output_json('200', '測試圖靈機器人返回結(jié)果', $callback);
}
}
Guzzle client->post 函數(shù)還是很簡單的,只需要訪問的接口良姆,和請求的參數(shù)肠虽,參數(shù)中主要包含:body、headers歇盼、query等舔痕,具體可參考
http://guzzle-cn.readthedocs.io/zh_CN/latest/quickstart.html#id8
測試下:
注:圖靈機器人還是很智能的评抚,根據(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ù):
然后再輸出 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"}}}
最后收到短信信息:
發(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ù)呛牲,使用 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();
總結(jié)
有了 Guzzle,極大方便了我們并發(fā)異步請求第三方接口驮配。如果時間允許娘扩,我們可以看看 Guzzle 源代碼,看看是如何實現(xiàn)的僧凤。
推薦
1.
在 windows 環(huán)境下畜侦,解決[GuzzleHttp\Exception\RequestException] cURL error 60: SSL certificate problem: unable to get local issuer certific ate
訪問這個網(wǎng)址 https://curl.haxx.se/ca/cacert.pem 下載文件
然后修改 php.ini curl.cainfo = "D:\cacert.pem" cacert.pem文件 隨便放在哪,沒限制躯保。
2.
需要了解 Guzzle 更多資料
查看官網(wǎng): http://guzzle-cn.readthedocs.io/zh_CN/latest/index.html
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 期待您繼續(xù)關(guān)注
也很感謝您能看到這了