Skip to content

Instantly share code, notes, and snippets.

@kerbeh
Created July 8, 2019 08:01
Show Gist options
  • Save kerbeh/0a452907722a7f403113d7b3edd222d9 to your computer and use it in GitHub Desktop.
Save kerbeh/0a452907722a7f403113d7b3edd222d9 to your computer and use it in GitHub Desktop.
Simple useage of GuzzleHttps request pools for concurrently running HTTP requests and returning responses for processing.
//One liner to batch an array of request objects
var_dump(Pool::batch($this->client, [$dropboxRequest, $quizRequest]));
die;
// Apply a function on fulfilled/rejected promises
// Mind the scope of the anonymous functions
$responses = [];
$errors = [];
$pool = new Pool($client, [$dropboxRequest, $quizRequest], [
'rejected' => function ($reason, $index) use (&$errors) {
return $errors[$index] = $reason;
},
'fulfilled' => function($response, $index) use (&$responses) {
return $responses[$index] = $response;
}
]);
// Initiate the transfers and create a promise
$promise = $pool->promise();
// Wait for the promise to resolve
$promise->wait();
var_dump($responses, $errors, $promise);
die;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment