-
-
Save IftiMahmud/90ec68f4031bad00f11d to your computer and use it in GitHub Desktop.
Concurrent requests using pecl_http
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 | |
$endpoint = "http://api.someservice.com"; | |
$userId = 101; | |
$urls = array( | |
$endpoint . '/profile/' . $userId, | |
$endpoint . '/orderHistory/' . $userId, | |
$endpoint . '/currentBalance/' . $userId | |
); | |
$pool = new HttpRequestPool; | |
foreach ($urls as $url) { | |
$req = new HttpRequest($url, HTTP_METH_GET); | |
$pool->attach($req); | |
} | |
// send all the requests. control is back to the script once | |
// all the requests are complete or timed out | |
$pool->send(); | |
foreach ($pool as $request) { | |
echo $request->getUrl(), PHP_EOL; | |
echo $request->getResponseBody(), PHP_EOL . PHP_EOL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment