我正在尝试一次获取多个数据请求以加快处理时间。我已经能够通过循环请求一次获得所需的所有数据,但需要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] =&gt; [1] =&gt; [2] =&gt;) 总时间:0.4572秒