PHP使用Guzzle發起的異步請求示例詳解
Guzzle是一個PHP的HTTP客戶端,它在發起http請求時不僅可以同步發起,還可以異步發起。
$client = new Client();$request = new Request('GET', 'http://www.baidu.com');$promise = $client->sendAsync($request)->then(function ($response) { echo $response->getBody();});// todo somethingecho 1;$promise->wait();PHP發起HTTP請求的幾種方式curl使用libcurl庫,允許你與各種的服務器使用各種類型的協議進行連接和通訊。
stream通過流的方式獲取和發送遠程文件,該功能需要ini配置allow_url_fopen=on。關于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會優先使用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,統一資源標識符。
scheme:[//[user:password@]host[:port]][/]path[?query][#fragment]
Guzzle發起請求大致分為兩個階段,第一階段負責將需要請求的uri組裝成各種內部定義的類。
Client類:這是一個發起客戶端的調用者,后續所有的調用需要基于這個負責的類實現,它負責提供一個 handler ,這是一個客戶端發起http請求的句柄,其中Guzzle實現curl和stream調用的無感知就是在這里實現的,同時開發者也可以自定義請求協議。// 根據系統當前狀態,選擇一個發起Http請求的協議的方法句柄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類:負責定義一個uriPromise類:這個類負責承載類請求發起前的各種準備工作完成后的結果,還包括兩個回調(請求成功回調、請求失敗回調),同時請求發起中的隊列,延遲等處理也是在這個類里。其中組裝階段最重要的方法是私有方法 private function transfer(RequestInterface $request, array $options) ,它負責將用戶通過各種方法傳入的uri和client類的各種屬性組合,然后使用這些屬性生成一個新的類 Promise 類。
請求的發起Client的各種屬性組裝完成后就可以使用得到的Promise類發起http請求了,這里主要是通過一個 wait() 方法。
同步調用與異步調用在同步方法內部的調用,同步方法是在內部組裝好一個Promise之后立刻發起wait()調用。
public function send(RequestInterface $request, array $options = []) {$options[RequestOptions::SYNCHRONOUS] = true;return $this->sendAsync($request, $options)->wait(); }wait的實現wait() 方法的實現邏輯也很簡單,遞歸調用wait()方法,直到result屬性不是PromiseInterface實現類或者state不是pending,然后將結果逐層輸出。這里說一下這個state的pending狀態,這是一個PromiseInterface實現類的初始化狀態,表示改實現類還沒有完成,需要繼續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狀態就執行。主要是執行改實現類的waitFn方法。最外層promise執行完成后執行queue()->run() `` 這個方法內部循環執行隊列內方法,直到隊列為空。至此,Guzzle就能將組裝進來的多個request,和各種方法執行完畢。
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() 函數。
$handler = $options['handler'];// 返回一個promise類,這個類有一個屬性是waitFnreturn Promise\promise_for($handler($request, $options));這里我們看 $handler 是什么?它是一個HandleStack類,就是我們在new Client時選擇的發起Http請求的協議的方法句柄,實例化的類。<br />之后的調用依次是 HandleStack->__invoke、RedirectMiddleware->__invoke、PrepareBodyMiddleware->__invoke。執行 $fn($request, $options); 方法,經過前面的逐層處理,此時的$fn就是HandleStack內部的Proxy包裝的方法,無論使用哪種協議都會在各自的實現里實例化一個擁有waitFn的Promise的實例。
// curl的實現$promise = new Promise( [$this, 'execute'], function () use ($id) {return $this->cancel($id); });由此可以直到waitFn方法就是各自協議的實現類的請求發起方法。then() 方法會將promise本身再封裝一層promise,并將原先的waitFn和then()的回調方法打包進waitFnList屬性里。
queue() 是的入隊時機當請求執行完成后依次調用 processMessages()、promise->resolve()、settle()、FulfilledPromise->then(),將請求結果插入隊列。
$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發起的異步請求示例詳解的詳細內容,更多關于PHP Guzzle異步請求的資料請關注好吧啦網其它相關文章!
