谷歌货币转换器api用于大量数据
I am using google finance calculator to convert currency. I have to convert atleast = 2000 numbers to be converted at once. Below I have set only 10 numbers in the array. When I run this code, it is working fine but it is taking too much time. When I try with around 5000 numbers, it sometimes time out error. Can anybody help me how can I modify with my code for large number of datas. Thank you. Below is code that I am currently using.
$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;
}
我正在使用谷歌金融计算器转换货币。 我必须转换至少= 2000个数字才能立即转换。 下面我在数组中只设置了10个数字。 当我运行此代码时,它工作正常,但它花费了太多时间。 当我尝试使用大约5000个数字时,它有时会超时错误。 任何人都可以帮助我如何使用我的代码修改大量数据。 谢谢。 下面是我目前使用的代码。 p>
$ amount = array(10,20,30,40,50,60,70,80,90,100); \ n $ from_Curr ='USD';
$ to_Curr ='THB';
$ convertedCurrency = convertCurrency($ amount,$ from_Curr,$ to_Curr);
print_r($ convertedCurrency);
function convertCurrency($ amount = array(),$ from,$ to){
$ convertedCurrency = array();
foreach($ amount as $ amount){
$ url =“https://www.google.com/finance/converter ?a = $ amount&amp; from = $ from&amp; to = $ to“;
$ data = file_get_contents($ url);
preg_match(”/&lt; span class = bld&gt;(。*)&lt; \ / span&gt ; /“,$ data,$ converted);
$ converted = preg_replace(”/ [^ 0-9。] /“,”“,$ converted [1]);
array_push($ convertedCurrency,round($ 转换,3));
}
返回$ convertedCurrency;
}
code> pre>
div>
Let's imagine a request gets anwered in 50ms, you can do the calculation yourself for 5000 requests...
Change the method of approaching the problem : exchange rates are linear, so you just need one answer, and you can do after the exchange calculation without the API. A multiplication operation is much faster than an API request. Ask the API for the counter value of 1 USD, and then multiply each USD amount by the obtained value :
$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($amounts);
print_r($convertedCurrency);
function convertCurrency($amounts = array(), $from, $to) {
$convertedCurrency = array();
$amount = 1;
$url = "https://finance.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]);
foreach ($amounts as $amount) {
$convertedCurrency[] = $amount * $converted;
}
return $convertedCurrency;
}
Sidenote: changed the regex to a non greedy match (.*?), in case spans are added lataer in the page.
You are using a free service right ? that's probably why you are getting a timeout error. there's a limit for requesting a response from the service which is why you get an error when that limit is reached. e.i how many requests over a specific period of time
You either look for a paid service that can intake your requirements ( 5000 responses at once )
Or change your request to a lesser number of times ( lesser than 5000).
for the code itself , I know that foreach is considered slow, try another looping solution.