亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將 id 添加到 Guzzle 請求

將 id 添加到 Guzzle 請求

PHP
狐的傳說 2022-12-03 10:42:40
如何向池中發送的 guzzle 請求添加一些元數據?我需要為請求添加 id 以確定將哪個請求分配給響應。我想要這樣的東西 - 將 id 設置為請求并從響應中獲取它。將 id 設置為 Header 似乎不是個好主意。請幫忙。    $requests = function ($total) {    $uri = 'http://127.0.0.1:8126/guzzle-server/perf';    for ($i = 0; $i < $total; $i++) {         $myAdditionalId = $i;        yield new Request('GET', $uri, $myAdditionalId);    }};    $pool = new Pool($client, $requests(100), [        'concurrency' => 5,        'fulfilled' => function (Response $response, $index) {             echo $response->getMyAdditionalId();            // this is delivered each successful response        },        'rejected' => function (RequestException $reason, $index) {            // this is delivered each failed request        },]);
查看完整描述

1 回答

?
LEATH

TA貢獻1936條經驗 獲得超7個贊

我以這種方式解決這個問題:


$requests = function ($total) {

    $uri = 'http://127.0.0.1:8126/guzzle-server/perf';

    for ($i = 0; $i < $total; $i++) {

         $myAdditionalId = $i;

            $promise =  $client->getAsync($uri);

         $promise->then(function () use ($myAdditionalId){

         //add callback with function on success

         echo $myAdditionalId;

          });

        yield function()use($promise){return $promise;};

    }

};


    $pool = new Pool($client, $requests(100), [

        'concurrency' => 5,

        'rejected' => function (RequestException $reason, $index) {

            // this is delivered each failed request

        },


]);


查看完整回答
反對 回復 2022-12-03
  • 1 回答
  • 0 關注
  • 94 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號