Created
July 8, 2019 08:01
-
-
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.
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
//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