Skip to content

Instantly share code, notes, and snippets.

@phpfour
Last active October 2, 2015 21:58
Show Gist options
  • Save phpfour/2330325 to your computer and use it in GitHub Desktop.
Save phpfour/2330325 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