我使用谷歌财务计算器来转换货币。我必须转换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