国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

PHP使用Guzzle發(fā)起的異步請求示例詳解

瀏覽:39日期:2022-06-14 09:18:57
目錄Guzzle中的異步請求使用Guzzle發(fā)起異步請求PHP發(fā)起HTTP請求的幾種方式curlstream什么是URI?URI的組成請求的組裝請求的發(fā)起同步調(diào)用與異步調(diào)用wait的實(shí)現(xiàn)waitFn是什么queue() 是的入隊(duì)時(shí)機(jī)Guzzle中的異步請求使用Guzzle發(fā)起異步請求

Guzzle是一個(gè)PHP的HTTP客戶端,它在發(fā)起http請求時(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 somethingecho 1;$promise->wait();PHP發(fā)起HTTP請求的幾種方式curl

使用libcurl庫,允許你與各種的服務(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)識符。

scheme:[//[user:password@]host[:port]][/]path[?query][#fragment]

請求的組裝

Guzzle發(fā)起請求大致分為兩個(gè)階段,第一階段負(fù)責(zé)將需要請求的uri組裝成各種內(nèi)部定義的類。

Client類:這是一個(gè)發(fā)起客戶端的調(diào)用者,后續(xù)所有的調(diào)用需要基于這個(gè)負(fù)責(zé)的類實(shí)現(xiàn),它負(fù)責(zé)提供一個(gè) handler ,這是一個(gè)客戶端發(fā)起http請求的句柄,其中Guzzle實(shí)現(xiàn)curl和stream調(diào)用的無感知就是在這里實(shí)現(xiàn)的,同時(shí)開發(fā)者也可以自定義請求協(xié)議。// 根據(jù)系統(tǒng)當(dāng)前狀態(tài),選擇一個(gè)發(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類:負(fù)責(zé)定義一個(gè)uriPromise類:這個(gè)類負(fù)責(zé)承載類請求發(fā)起前的各種準(zhǔn)備工作完成后的結(jié)果,還包括兩個(gè)回調(diào)(請求成功回調(diào)、請求失敗回調(diào)),同時(shí)請求發(fā)起中的隊(duì)列,延遲等處理也是在這個(gè)類里。

其中組裝階段最重要的方法是私有方法 private function transfer(RequestInterface $request, array $options) ,它負(fù)責(zé)將用戶通過各種方法傳入的uri和client類的各種屬性組合,然后使用這些屬性生成一個(gè)新的類 Promise 類。

請求的發(fā)起

Client的各種屬性組裝完成后就可以使用得到的Promise類發(fā)起http請求了,這里主要是通過一個(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)邏輯也很簡單,遞歸調(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)來的多個(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è)屬性是waitFnreturn Promise\promise_for($handler($request, $options));

這里我們看 $handler 是什么?它是一個(gè)HandleStack類,就是我們在new Client時(shí)選擇的發(fā)起Http請求的協(xié)議的方法句柄,實(shí)例化的類。<br />之后的調(diào)用依次是 HandleStack->__invoke、RedirectMiddleware->__invoke、PrepareBodyMiddleware->__invoke。執(zhí)行 $fn($request, $options); 方法,經(jīng)過前面的逐層處理,此時(shí)的$fn就是HandleStack內(nèi)部的Proxy包裝的方法,無論使用哪種協(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)類的請求發(fā)起方法。then() 方法會(huì)將promise本身再封裝一層promise,并將原先的waitFn和then()的回調(diào)方法打包進(jìn)waitFnList屬性里。

queue() 是的入隊(duì)時(shí)機(jī)

當(dāng)請求執(zhí)行完成后依次調(diào)用 processMessages()、promise->resolve()、settle()、FulfilledPromise->then(),將請求結(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ā)起的異步請求示例詳解的詳細(xì)內(nèi)容,更多關(guān)于PHP Guzzle異步請求的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: PHP
主站蜘蛛池模板: 宾阳县| 丰城市| 德格县| 沽源县| 深水埗区| 彰武县| 塔城市| 从化市| 米泉市| 叶城县| 嘉定区| 东城区| 凤阳县| 兰溪市| 焉耆| 宜黄县| 彩票| 韶关市| 曲靖市| 江北区| 分宜县| 临潭县| 城口县| 秦皇岛市| 万盛区| 志丹县| 乳源| 揭东县| 斗六市| 寿宁县| 林西县| 霞浦县| 江华| 普格县| 广东省| 遵义市| 修文县| 荣成市| 中方县| 拜城县| 金乡县|