I have a PHP script that needs to download several files from a remote server. At the moment I just have a loop downloading and processing the files with cURL, which means t
The library of @stil is so, so cool. Many thanks to him!
Still, I have written nice utility function that makes it very easy to get asynchronously content from multiple URLs (APIs in my case) and to return them without losing information which is which.
You simply run it by passing key => value array as an input and it returns key => response array as result :- )
/**
* This function runs multiple GET requests parallely.
* @param array $urlsArray needs to be in format:
* array(
* [url_unique_id_1] => [url_for_request_1],
* [url_unique_id_2] => [url_for_request_2],
* [url_unique_id_3] => [url_for_request_3]
* )
* e.g. input like:
* array(
* "myemail@gmail.com" =>
* "http://someapi.com/results?search=easylife",
* "michael@gmail.com" =>
* "http://someapi.com/results?search=safelife"
* )
* @return array An array where for every url_unique_id response to this request
* is returned e.g.
* array(
* "myemail@gmail.com" =>
* "Work less, enjoy more",
* "michael@gmail.com" =>
* "Study, work, pay taxes"
* )
* */
public function getResponsesFromUrlsAsynchronously(array $urlsArray, $timeout = 8) {
$queue = new \cURL\RequestsQueue;
// Set default options for all requests in queue
$queue->getDefaultOptions()
->set(CURLOPT_TIMEOUT, $timeout)
->set(CURLOPT_RETURNTRANSFER, true);
// =========================================================================
// Define some extra variables to be used in callback
global $requestUidToUserUrlIdentifiers;
$requestUidToUserUrlIdentifiers = array();
global $userIdentifiersToResponses;
$userIdentifiersToResponses = array();
// =========================================================================
// Set function to be executed when request will be completed
$queue->addListener('complete', function (\cURL\Event $event) {
// Define user identifier for this url
global $requestUidToUserUrlIdentifiers;
$requestId = $event->request->getUID();
$userIdentifier = $requestUidToUserUrlIdentifiers[$requestId];
// =========================================================================
$response = $event->response;
$json = $response->getContent(); // Returns content of response
$apiResponseAsArray = json_decode($json, true);
$apiResponseAsArray = $apiResponseAsArray['jobs'];
// =========================================================================
// Store this response in proper structure
global $userIdentifiersToResponses;
$userIdentifiersToResponses[$userIdentifier] = $apiResponseAsArray;
});
// =========================================================================
// Add all request to queue
foreach ($urlsArray as $userUrlIdentifier => $url) {
$request = new \cURL\Request($url);
$requestUidToUserUrlIdentifiers[$request->getUID()] = $userUrlIdentifier;
$queue->attach($request);
}
// =========================================================================
// Execute queue
$queue->send();
// =========================================================================
return $userIdentifiersToResponses;
}