-
-
Save Luxato/764c1065828a4509b9d1cd768e5f9110 to your computer and use it in GitHub Desktop.
GuzzleHttp\Pool example: identifying responses to concurrent async requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Using a key => value pair with the yield keyword is | |
* the cleanest method I could find to add identifiers or tags | |
* to asynchronous concurrent requests in Guzzle, | |
* so you can identify which response is from which request! | |
*/ | |
$client = new GuzzleHttp\Client(['base_uri' => 'http://httpbin.org']); | |
$requestGenerator = function($searchTerms) use ($client) { | |
foreach($searchTerms as $searchTerm) { | |
// The magic happens here, with yield key => value | |
yield $searchTerm => function() use ($client, $searchTerm) { | |
// Our identifier does not have to be included in the request URI or headers | |
return $client->getAsync('/get?q='.$searchTerm, ['headers' => ['X-Search-Term' => $searchTerm]]); | |
}; | |
} | |
}; | |
$searchTerms = ['apple','banana','pear','orange','melon','grape','raisin']; | |
$pool = new GuzzleHttp\Pool($client, $requestGenerator($searchTerms), [ | |
'concurrency' => 3, | |
'fulfilled' => function(GuzzleHttp\Psr7\Response $response, $index) { | |
// This callback is delivered each successful response | |
// $index will be our special identifier we set when generating the request | |
$json = json_decode((string)$response->getBody()); | |
// If these values don't match, something is very wrong | |
echo "Requested search term: ", $index, "\n"; | |
echo "Parsed from response: ", $json->headers->{'X-Search-Term'}, "\n\n"; | |
}, | |
'rejected' => function(Exception $reason, $index) { | |
// This callback is delivered each failed request | |
echo "Requested search term: ", $index, "\n"; | |
echo $reason->getMessage(), "\n\n"; | |
}, | |
]); | |
// Initiate the transfers and create a promise | |
$promise = $pool->promise(); | |
// Force the pool of requests to complete | |
$promise->wait(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment