0

I am trying to implement this Google Finance Convertor in my project. I implemented it and it was working fine then. But today when I rechecked my application to ensure every thing is working properly I found out that the Google Finance Convertor API is not working and also returning an error.

PHP Code

/*********** Google Finance API ************/
  $from_Currency = "USD";
  $to_Currency = "INR";
  $encode_amount = $amount;
  $get = file_get_contents("http://finance.google.com/finance/converter?a=$encode_amount&from=$from_Currency&to=$to_Currency");
  $get = explode("<span class=bld>",$get);
  $get = explode("</span>",$get[1]);
  $converted_currency = preg_replace("/[^0-9\.]/", null, $get[0]);
  /*********** End Google Finance API ************/

It returns something like Undefined offset: 1

Complete Error Code

 Notice: Undefined offset: 1 in E:\xampp\htdocs\sites\redapple\gateways\payumoney\payumoney.php on line 31

Here in my code this line represents line number 31 $get = explode("</span>",$get[1]);

Please help.

2 Answers2

1

Its because google has stopped providing finace api even finance.google.com takes us to google finance tab

0

I used this Google API too. Thanks to duckduckgo (they have a currency converter) found the xe.com website. As Gurpreet Singh suggested.

This is my reworked version. Obviously it's part of class.

private static function convertCurrency($amount, $from, $to) {
    $url  = "https://www.xe.com/currencyconverter/convert/?Amount={$amount}&From={$from}&To={$to}";
    $data = @file_get_contents($url);
    if ( ! $data)
    {
        return null;
    }
    preg_match_all("/<span class='uccResultAmount'>([0-9.]*)<\/span>/", $data, $converted);
    try
    {
        $final = $converted[1][0];
    } catch (\Exception $e)
    {
        return null;
    }

    return round($final, 2);
}

Let's hope this keeps working for a while. Other (free) solutions are bound to USD to limited currencies. What if you need Euro to Philippine Peso?

Dimitri Mostrey
  • 2,302
  • 1
  • 12
  • 11