Last active
May 28, 2021 15:15
-
-
Save ardani/a7b2557fb74bcff006ed7e2040a2b553 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What is the fastest way to send 100,000 HTTP requests to PHP and retrieve data from them?