PHP使用Guzzle發(fā)起的異步請求示例詳解
Guzzle中的異步請求
使用Guzzle發(fā)起異步請求
Guzzle是一個PHP的HTTP客戶端,它在發(fā)起http請求時不僅可以同步發(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請求的幾種方式
curl
使用libcurl庫,允許你與各種的服務器使用各種類型的協(xié)議進行連接和通訊。
stream
通過流的方式獲取和發(fā)送遠程文件,該功能需要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會優(yōu)先使用curl,然后選擇使用stream,Proxy::wrapStreaming($handler, new StreamHandler())
是一個流包裝器。
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)一資源標識符。
scheme:[//[user:password@]host[:port]][/]path[?query][#fragment]
請求的組裝
Guzzle發(fā)起請求大致分為兩個階段,第一階段負責將需要請求的uri組裝成各種內(nèi)部定義的類。
- Client類:這是一個發(fā)起客戶端的調(diào)用者,后續(xù)所有的調(diào)用需要基于這個負責的類實現(xiàn),它負責提供一個 handler ,這是一個客戶端發(fā)起http請求的句柄,其中Guzzle實現(xiàn)curl和stream調(diào)用的無感知就是在這里實現(xiàn)的,同時開發(fā)者也可以自定義請求協(xié)議。
// 根據(jù)系統(tǒng)當前狀態(tài),選擇一個發(fā)起Http請求的協(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類:負責定義一個uri
- Promise類:這個類負責承載類請求發(fā)起前的各種準備工作完成后的結(jié)果,還包括兩個回調(diào)(請求成功回調(diào)、請求失敗回調(diào)),同時請求發(fā)起中的隊列,延遲等處理也是在這個類里。
其中組裝階段最重要的方法是私有方法 private function transfer(RequestInterface $request, array $options)
,它負責將用戶通過各種方法傳入的uri和client類的各種屬性組合,然后使用這些屬性生成一個新的類 Promise 類。
請求的發(fā)起
Client的各種屬性組裝完成后就可以使用得到的Promise類發(fā)起http請求了,這里主要是通過一個 wait() 方法。
同步調(diào)用與異步調(diào)用
在同步方法內(nèi)部的調(diào)用,同步方法是在內(nèi)部組裝好一個Promise之后立刻發(fā)起wait()調(diào)用。
public function send(RequestInterface $request, array $options = []) { $options[RequestOptions::SYNCHRONOUS] = true; return $this->sendAsync($request, $options)->wait(); }
wait的實現(xiàn)
wait() 方法的實現(xiàn)邏輯也很簡單,遞歸調(diào)用wait()方法,直到result屬性不是PromiseInterface實現(xiàn)類或者state不是pending,然后將結(jié)果逐層輸出。這里說一下這個state的pending狀態(tài),這是一個PromiseInterface實現(xiàn)類的初始化狀態(tài),表示改實現(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í)行改實現(xiàn)類的waitFn方法。最外層promise執(zhí)行完成后執(zhí)行queue()->run()
`` 這個方法內(nèi)部循環(huán)執(zhí)行隊列內(nèi)方法,直到隊列為空。至此,Guzzle就能將組裝進來的多個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']; // 返回一個promise類,這個類有一個屬性是waitFn return Promise\promise_for($handler($request, $options));
這里我們看 $handler 是什么?它是一個HandleStack類,就是我們在new Client時選擇的發(fā)起Http請求的協(xié)議的方法句柄,實例化的類。<br />之后的調(diào)用依次是 HandleStack->__invoke、RedirectMiddleware->__invoke、PrepareBodyMiddleware->__invoke。執(zhí)行 $fn($request, $options);
方法,經(jīng)過前面的逐層處理,此時的$fn就是HandleStack內(nèi)部的Proxy包裝的方法,無論使用哪種協(xié)議都會在各自的實現(xiàn)里實例化一個擁有waitFn的Promise的實例。
// curl的實現(xiàn) $promise = new Promise( [$this, 'execute'], function () use ($id) { return $this->cancel($id); } );
由此可以直到waitFn方法就是各自協(xié)議的實現(xiàn)類的請求發(fā)起方法。then() 方法會將promise本身再封裝一層promise,并將原先的waitFn和then()的回調(diào)方法打包進waitFnList屬性里。
queue() 是的入隊時機
當請求執(zhí)行完成后依次調(diào)用 processMessages()、promise->resolve()、settle()、FulfilledPromise->then(),將請求結(jié)果插入隊列。
$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ā)起的異步請求示例詳解的詳細內(nèi)容,更多關(guān)于PHP Guzzle異步請求的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
用php和jQuery來實現(xiàn)“頂”和“踩”的投票功能
本文結(jié)合實例,講解使用PHP+MySql+jQuery實現(xiàn)的“頂”和“踩”投票功能,判斷用戶的投票行為是否有效。2016-10-10php實現(xiàn)信用卡校驗位算法THE LUHN MOD-10示例
這篇文章主要介紹了php實現(xiàn)信用卡校驗位算法THE LUHN MOD-10的示例,需要的朋友可以參考下2014-05-05set_exception_handler函數(shù)在ThinkPHP中的用法
這篇文章主要介紹了set_exception_handler函數(shù)在ThinkPHP中的用法,分析了官方給出了set_exception_handler函數(shù)用法說明及示例,并講述了在ThinkPHP中的應用實例,需要的朋友可以參考下2014-10-10Zend Framework框架中實現(xiàn)Ajax的方法示例
這篇文章主要介紹了Zend Framework框架中實現(xiàn)Ajax的方法,結(jié)合實例形式詳細分析了Zend Framework框架中實現(xiàn)ajax功能的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-06-06PHP二維關(guān)聯(lián)數(shù)組的遍歷方式(實例講解)
下面小編就為大家?guī)硪黄狿HP二維關(guān)聯(lián)數(shù)組的遍歷方式(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10