Skip to content
Advertisement

Add id to Guzzle Request

How can I add some meta data to guzzle request sends in Pool? I need to add id for request to determine which request assign to response. I want something like this – set id to request and get it from response.Set id to Header seems not to be good idea. Please help.

    $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
        },

]);

Advertisement

Answer

I solve this problem in that way:

$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
        },

]);
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement