PHP Несколько одновременных запросов данных

Я пытаюсь получить сразу несколько запросов данных, чтобы ускорить время обработки. Я смог получить все нужные мне данные, запросив по очереди запросы, но это занимает 30-60 секунд. Я только что обнаружил curl_multi, но я не могу заставить его возвращать данные, и поэтому в новом коде ниже я пытаюсь получить 3 URL-адреса для работы, но в итоге это будет, вероятно, 10+ запросов одновременно. Я отредактировал свои ключи и заменил их на xxxx, поэтому я уже знаю, что он не будет работать как есть, если вы попробуете.

Любая помощь или руководство очень ценится!

Код, который работает для всех запросов, но требует много времени:

define(TIME, date('U')+3852); require_once 'OAuth.php'; $consumer_key = 'xxxx'; $consumer_secret = 'xxxx'; $access_token = 'xxxx'; $access_secret = 'xxxx'; $i=0; foreach ($expirations as $row){ $expiry_date = $expirations[$i]; $url = "https://api.tradeking.com/v1/market/options/search.xml?symbol=SPX&query=xdate-eq%3A$expiry_date"; //Option Expirations //$url = 'https://api.tradeking.com/v1/market/options/expirations.xml?symbol=SPX'; //Account data //$url = 'https://api.tradeking.com/v1/accounts'; $consumer = new OAuthConsumer($consumer_key,$consumer_secret); $access = new OAuthToken($access_token,$access_secret); $request = OAuthRequest::from_consumer_and_token($consumer, $access, 'GET', $url); $request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, $access); $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_HTTPHEADER, array($request->to_header())); $response = curl_exec($ch); //Turn XML ($response) into an array ($array) $array=json_decode(json_encode(simplexml_load_string($response)),true); //Trim excess fields from ($array) and define as ($chain) $chain = $array['quotes']['quote']; $bigarray[$i] = $chain; $i++; } 

Возвращает огромный массив как переменную $ bigarray

Код, который не работает для трех одновременных запросов:

 define(TIME, date('U')+3852); require_once 'OAuth.php'; $consumer_key = 'xxxx'; $consumer_secret = 'xxxx'; $access_token = 'xxxx'; $access_secret = 'xxxx'; $consumer = new OAuthConsumer($consumer_key,$consumer_secret); $access = new OAuthToken($access_token,$access_secret); // Run the parallel get and print the total time $s = microtime(true); // Define the URLs $urls = array( "https://api.tradeking.com/v1/market/options/search.xml?symbol=SPX&query=xdate-eq%3A$20160311", "https://api.tradeking.com/v1/market/options/search.xml?symbol=SPX&query=xdate-eq%3A$20160318", "https://api.tradeking.com/v1/market/options/search.xml?symbol=SPX&query=xdate-eq%3A$20160325" ); $pg = new ParallelGet($urls); print "<br />Total time: ".round(microtime(true) - $s, 4)." seconds"; // Class to run parallel GET requests and return the transfer class ParallelGet { function __construct($urls) { // Create get requests for each URL $mh = curl_multi_init(); foreach($urls as $i => $url) { $request = OAuthRequest::from_consumer_and_token($consumer, $access, 'GET', $url); $request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, $access); $ch[$i] = curl_init($url); curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, true); curl_setopt($ch[$i], CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch[$i], CURLOPT_HTTPHEADER, array($request->to_header())); curl_multi_add_handle($mh, $ch[$i]); } // Start performing the request do { $execReturnValue = curl_multi_exec($mh, $runningHandles); } while ($execReturnValue == CURLM_CALL_MULTI_PERFORM); // Loop and continue processing the request while ($runningHandles && $execReturnValue == CURLM_OK) { // Wait forever for network $numberReady = curl_multi_select($mh); if ($numberReady != -1) { // Pull in any new data, or at least handle timeouts do { $execReturnValue = curl_multi_exec($mh, $runningHandles); } while ($execReturnValue == CURLM_CALL_MULTI_PERFORM); } } // Check for any errors if ($execReturnValue != CURLM_OK) { trigger_error("Curl multi read error $execReturnValue\n", E_USER_WARNING); } // Extract the content foreach($urls as $i => $url) { // Check for errors $curlError = curl_error($ch[$i]); if($curlError == "") { $res[$i] = curl_multi_getcontent($ch[$i]); } else { print "Curl error on handle $i: $curlError\n"; } // Remove and close the handle curl_multi_remove_handle($mh, $ch[$i]); curl_close($ch[$i]); } // Clean up the curl_multi handle curl_multi_close($mh); // Print the response data print_r($res); } } ?> 

Возвраты: Массив ([0] => [1] => [2] =>) Общее время: 0,4572 секунды