Skip to content

Instantly share code, notes, and snippets.

@IftiMahmud
Forked from phpfour/http_pool.php
Last active August 29, 2015 14:27
Show Gist options
  • Save IftiMahmud/90ec68f4031bad00f11d to your computer and use it in GitHub Desktop.
Save IftiMahmud/90ec68f4031bad00f11d to your computer and use it in GitHub Desktop.
Concurrent requests using pecl_http
<?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