为了账号安全,请及时绑定邮箱和手机立即绑定

将 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 关注
  • 148 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号