PHP使用Guzzle發(fā)起的異步請(qǐng)求示例詳解
Guzzle中的異步請(qǐng)求
使用Guzzle發(fā)起異步請(qǐng)求
Guzzle是一個(gè)PHP的HTTP客戶端,它在發(fā)起http請(qǐng)求時(shí)不僅可以同步發(fā)起,還可以異步發(fā)起。
$client = new Client();
$request = new Request('GET', 'http://www.baidu.com');
$promise = $client->sendAsync($request)->then(function ($response) {
echo $response->getBody();
});
// todo something
echo 1;
$promise->wait();PHP發(fā)起HTTP請(qǐng)求的幾種方式
curl
使用libcurl庫(kù),允許你與各種的服務(wù)器使用各種類型的協(xié)議進(jìn)行連接和通訊。
stream
通過流的方式獲取和發(fā)送遠(yuǎn)程文件,該功能需要ini配置allow_url_fopen=on。關(guān)于php的流更多參考PHP流(Stream)的概述與使用詳解
在guzzle中可以兼容使用這兩種的任意一種或者是用戶自定義的http handler
function choose_handler()
{
$handler = null;
if (function_exists('curl_multi_exec') && function_exists('curl_exec')) {
$handler = Proxy::wrapSync(new CurlMultiHandler(), new CurlHandler());
} elseif (function_exists('curl_exec')) {
$handler = new CurlHandler();
} elseif (function_exists('curl_multi_exec')) {
$handler = new CurlMultiHandler();
}
if (ini_get('allow_url_fopen')) {
$handler = $handler
? Proxy::wrapStreaming($handler, new StreamHandler())
: new StreamHandler();
} elseif (!$handler) {
throw new \RuntimeException('GuzzleHttp requires cURL, the '
. 'allow_url_fopen ini setting, or a custom HTTP handler.');
}
return $handler;
}可以看出,guzzle會(huì)優(yōu)先使用curl,然后選擇使用stream,Proxy::wrapStreaming($handler, new StreamHandler()) 是一個(gè)流包裝器。
public static function wrapStreaming(
callable $default,
callable $streaming
) {
return function (RequestInterface $request, array $options) use ($default, $streaming) {
return empty($options['stream'])
? $default($request, $options)
: $streaming($request, $options);
};
}什么是URI?URI的組成
URI,Uniform Resource Identifier,統(tǒng)一資源標(biāo)識(shí)符。
scheme:[//[user:password@]host[:port]][/]path[?query][#fragment]

請(qǐng)求的組裝
Guzzle發(fā)起請(qǐng)求大致分為兩個(gè)階段,第一階段負(fù)責(zé)將需要請(qǐng)求的uri組裝成各種內(nèi)部定義的類。
- Client類:這是一個(gè)發(fā)起客戶端的調(diào)用者,后續(xù)所有的調(diào)用需要基于這個(gè)負(fù)責(zé)的類實(shí)現(xiàn),它負(fù)責(zé)提供一個(gè) handler ,這是一個(gè)客戶端發(fā)起http請(qǐng)求的句柄,其中Guzzle實(shí)現(xiàn)curl和stream調(diào)用的無(wú)感知就是在這里實(shí)現(xiàn)的,同時(shí)開發(fā)者也可以自定義請(qǐng)求協(xié)議。
// 根據(jù)系統(tǒng)當(dāng)前狀態(tài),選擇一個(gè)發(fā)起Http請(qǐng)求的協(xié)議的方法句柄
function choose_handler()
{
$handler = null;
if (function_exists('curl_multi_exec') && function_exists('curl_exec')) {
$handler = Proxy::wrapSync(new CurlMultiHandler(), new CurlHandler());
} elseif (function_exists('curl_exec')) {
$handler = new CurlHandler();
} elseif (function_exists('curl_multi_exec')) {
$handler = new CurlMultiHandler();
}
if (ini_get('allow_url_fopen')) {
$handler = $handler
? Proxy::wrapStreaming($handler, new StreamHandler())
: new StreamHandler();
} elseif (!$handler) {
throw new \RuntimeException('GuzzleHttp requires cURL, the '
. 'allow_url_fopen ini setting, or a custom HTTP handler.');
}
return $handler;
}- Request類:負(fù)責(zé)定義一個(gè)uri
- Promise類:這個(gè)類負(fù)責(zé)承載類請(qǐng)求發(fā)起前的各種準(zhǔn)備工作完成后的結(jié)果,還包括兩個(gè)回調(diào)(請(qǐng)求成功回調(diào)、請(qǐng)求失敗回調(diào)),同時(shí)請(qǐng)求發(fā)起中的隊(duì)列,延遲等處理也是在這個(gè)類里。
其中組裝階段最重要的方法是私有方法 private function transfer(RequestInterface $request, array $options) ,它負(fù)責(zé)將用戶通過各種方法傳入的uri和client類的各種屬性組合,然后使用這些屬性生成一個(gè)新的類 Promise 類。
請(qǐng)求的發(fā)起
Client的各種屬性組裝完成后就可以使用得到的Promise類發(fā)起http請(qǐng)求了,這里主要是通過一個(gè) wait() 方法。
同步調(diào)用與異步調(diào)用
在同步方法內(nèi)部的調(diào)用,同步方法是在內(nèi)部組裝好一個(gè)Promise之后立刻發(fā)起wait()調(diào)用。
public function send(RequestInterface $request, array $options = [])
{
$options[RequestOptions::SYNCHRONOUS] = true;
return $this->sendAsync($request, $options)->wait();
}wait的實(shí)現(xiàn)
wait() 方法的實(shí)現(xiàn)邏輯也很簡(jiǎn)單,遞歸調(diào)用wait()方法,直到result屬性不是PromiseInterface實(shí)現(xiàn)類或者state不是pending,然后將結(jié)果逐層輸出。這里說一下這個(gè)state的pending狀態(tài),這是一個(gè)PromiseInterface實(shí)現(xiàn)類的初始化狀態(tài),表示改實(shí)現(xiàn)類還沒有完成,需要繼續(xù)wait。
public function wait($unwrap = true)
{
$this->waitIfPending();
$inner = $this->result instanceof PromiseInterface
? $this->result->wait($unwrap)
: $this->result;
if ($unwrap) {
if ($this->result instanceof PromiseInterface
|| $this->state === self::FULFILLED
) {
return $inner;
} else {
// It's rejected so "unwrap" and throw an exception.
throw exception_for($inner);
}
}
}waitIfPending() : 如果promise類還處于pending狀態(tài)就執(zhí)行。主要是執(zhí)行改實(shí)現(xiàn)類的waitFn方法。最外層promise執(zhí)行完成后執(zhí)行queue()->run() `` 這個(gè)方法內(nèi)部循環(huán)執(zhí)行隊(duì)列內(nèi)方法,直到隊(duì)列為空。至此,Guzzle就能將組裝進(jìn)來(lái)的多個(gè)request,和各種方法執(zhí)行完畢。
private function waitIfPending()
{
if ($this->state !== self::PENDING) {
return;
} elseif ($this->waitFn) {
$this->invokeWaitFn();
} elseif ($this->waitList) {
$this->invokeWaitList();
} else {
// If there's not wait function, then reject the promise.
$this->reject('Cannot wait on a promise that has '
. 'no internal wait function. You must provide a wait '
. 'function when constructing the promise to be able to '
. 'wait on a promise.');
}
queue()->run();
if ($this->state === self::PENDING) {
$this->reject('Invoking the wait callback did not resolve the promise');
}
}
public function run()
{
/** @var callable $task */
while ($task = array_shift($this->queue)) {
$task();
}
}waitFn是什么
回到前面提到的transfer() 函數(shù)。
$handler = $options['handler']; // 返回一個(gè)promise類,這個(gè)類有一個(gè)屬性是waitFn return Promise\promise_for($handler($request, $options));
這里我們看 $handler 是什么?它是一個(gè)HandleStack類,就是我們?cè)趎ew Client時(shí)選擇的發(fā)起Http請(qǐng)求的協(xié)議的方法句柄,實(shí)例化的類。<br />之后的調(diào)用依次是 HandleStack->__invoke、RedirectMiddleware->__invoke、PrepareBodyMiddleware->__invoke。執(zhí)行 $fn($request, $options); 方法,經(jīng)過前面的逐層處理,此時(shí)的$fn就是HandleStack內(nèi)部的Proxy包裝的方法,無(wú)論使用哪種協(xié)議都會(huì)在各自的實(shí)現(xiàn)里實(shí)例化一個(gè)擁有waitFn的Promise的實(shí)例。
// curl的實(shí)現(xiàn)
$promise = new Promise(
[$this, 'execute'],
function () use ($id) {
return $this->cancel($id);
}
);由此可以直到waitFn方法就是各自協(xié)議的實(shí)現(xiàn)類的請(qǐng)求發(fā)起方法。then() 方法會(huì)將promise本身再封裝一層promise,并將原先的waitFn和then()的回調(diào)方法打包進(jìn)waitFnList屬性里。
queue() 是的入隊(duì)時(shí)機(jī)
當(dāng)請(qǐng)求執(zhí)行完成后依次調(diào)用 processMessages()、promise->resolve()、settle()、FulfilledPromise->then(),將請(qǐng)求結(jié)果插入隊(duì)列。
$queue->add(static function () use ($p, $value, $onFulfilled) {
if ($p->getState() === self::PENDING) {
try {
$p->resolve($onFulfilled($value));
} catch (\Throwable $e) {
$p->reject($e);
} catch (\Exception $e) {
$p->reject($e);
}
}
});以上就是PHP使用Guzzle發(fā)起的異步請(qǐng)求示例詳解的詳細(xì)內(nèi)容,更多關(guān)于PHP Guzzle異步請(qǐng)求的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
用php和jQuery來(lái)實(shí)現(xiàn)“頂”和“踩”的投票功能
本文結(jié)合實(shí)例,講解使用PHP+MySql+jQuery實(shí)現(xiàn)的“頂”和“踩”投票功能,判斷用戶的投票行為是否有效。2016-10-10
php實(shí)現(xiàn)信用卡校驗(yàn)位算法THE LUHN MOD-10示例
這篇文章主要介紹了php實(shí)現(xiàn)信用卡校驗(yàn)位算法THE LUHN MOD-10的示例,需要的朋友可以參考下2014-05-05
set_exception_handler函數(shù)在ThinkPHP中的用法
這篇文章主要介紹了set_exception_handler函數(shù)在ThinkPHP中的用法,分析了官方給出了set_exception_handler函數(shù)用法說明及示例,并講述了在ThinkPHP中的應(yīng)用實(shí)例,需要的朋友可以參考下2014-10-10
PHP中將網(wǎng)頁(yè)導(dǎo)出為Word文檔的代碼
今天要探討的是PHP網(wǎng)頁(yè)導(dǎo)出Word文檔的方法,使用其他語(yǔ)言的朋友也可以參考,因?yàn)樵硎遣畈欢嗟?/div> 2012-05-05
Zend Framework框架中實(shí)現(xiàn)Ajax的方法示例
這篇文章主要介紹了Zend Framework框架中實(shí)現(xiàn)Ajax的方法,結(jié)合實(shí)例形式詳細(xì)分析了Zend Framework框架中實(shí)現(xiàn)ajax功能的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
PHP二維關(guān)聯(lián)數(shù)組的遍歷方式(實(shí)例講解)
下面小編就為大家?guī)?lái)一篇PHP二維關(guān)聯(lián)數(shù)組的遍歷方式(實(shí)例講解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-10-10最新評(píng)論

