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.
JavaScript
x
$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:
JavaScript
$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
},
]);