我使用谷歌財務計算器來轉換貨幣。我必須轉換atleast = 2000數字一次轉換。下面我在數組中只設置了10個數字。當我運行這個代碼時,它運行良好,但它花費的時間太多。當我嘗試使用大約5000個數字時,它有時會超時錯誤。任何人都可以幫助我如何使用我的代碼修改大量數據。謝謝。以下是我目前使用的代碼。谷歌貨幣轉換器API的大量數據
$amounts= array(10, 20, 30, 40, 50, 60, 70, 80, 90, 100);
$from_Curr ='USD';
$to_Curr = 'THB';
$convertedCurrency = convertCurrency($amounts, $from_Curr, $to_Curr);
print_r($convertedCurrency);
function convertCurrency($amounts= array(), $from, $to){
$convertedCurrency = array();
foreach ($amounts as $amount) {
$url = "https://www.google.com/finance/converter?a=$amount&from=$from&to=$to";
$data = file_get_contents($url);
preg_match("/<span class=bld>(.*)<\/span>/",$data, $converted);
$converted = preg_replace("/[^0-9.]/", "", $converted[1]);
array_push($convertedCurrency, round($converted, 3));
}
return $convertedCurrency;
}
,雖然這是需要時間,但這次沒有響應時間。謝謝 – nas
@nas很高興幫助。您仍然需要時間向API發出一個請求:通常在100-150ms內回答。你不能爲此做很多事情。之後,5000個計算將快速完成。所花費的時間花在其他操作上,比如設置頁面並將數據發送到瀏覽器...... – Zimmi