I have a bunch of remote content providers whose APIs are queried to respond to a user request. Assuming every content provider needs to be queried, how would I issue the API requests asynchronously and collect the results? For now I am just iterating over my collection of content providers and call their API one by one like this:
/**
* A wrapper method that delegates requests to content providers.
*/
public function __call($name, $args) {
// do stuff before
$results = array();
foreach ($contentProviders as $contentProvider) {
$callback = array($contentProvider, $name), $args);
$results[get_class($contentProvider)] = call_user_func_array($callback);
}
// do stuff after
// build final result from individual calls
return $result;
}
This, however, leaves me waiting until a request is complete before starting the next one. Is there even a way to do this asynchronously in PHP?