Skip to content

Instantly share code, notes, and snippets.

@ardani
Last active May 28, 2021 15:15
Show Gist options
  • Save ardani/a7b2557fb74bcff006ed7e2040a2b553 to your computer and use it in GitHub Desktop.
Save ardani/a7b2557fb74bcff006ed7e2040a2b553 to your computer and use it in GitHub Desktop.
<?php
use GuzzleHttp\Promise\EachPromise;
use GuzzleHttp\Psr7\Response;
$users = ['one', 'two', 'three'];
$promises = (function () use ($users) {
foreach ($users as $user) {
// don't forget using generator
yield $this->getAsync('https://api.demo.com/v1/users?username=' . $user);
}
})();
$eachPromise = new EachPromise($promises, [
// how many concurrency we are use
'concurrency' => 4,
'fulfilled' => function (Response $response) {
if ($response->getStatusCode() == 200) {
$user = json_decode($response->getBody(), true);
// processing response of user here
}
},
'rejected' => function ($reason) {
// handle promise rejected here
}
]);
$eachPromise->promise()->wait();
@oleksandr-roskovynskyi
Copy link

What is the fastest way to send 100,000 HTTP requests to PHP and retrieve data from them?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment