nas
nas

Reputation: 2437

Google currency converter api for large number of data

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;
}

Upvotes: 2

Views: 2383

Answers (2)

Zimmi
Zimmi

Reputation: 1599

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.

Upvotes: 2

Pc hobbit
Pc hobbit

Reputation: 96

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.

Upvotes: 1

Related Questions