GOOGLE Finance converter api is not responding

4.8k views Asked by At

I have been trying to convert the currencies using google finance converter in PHP.

I have used the following code.

$amount = 100;
$from_Currency = "INR";
$to_Currency = "BTC";

 $amount = urlencode($amount);
  $from_Currency = urlencode($from_Currency);
  $to_Currency = urlencode($to_Currency);

$get = file_get_contents("https://finance.google.com/finance/converter?a=$amount&from=$from_Currency&to=$to_Currency&meta=ei%3DZsa7WeGkE4_RuASY95SQAw");

  $get = explode("<span class=bld>",$get);


  $get = explode("</span>",$get[1]);  

  $converted_amount = preg_replace("/[^0-9\.]/", null, $get[0]);
  echo ceil($converted_amount);
 ?>

But i am getting the following error

Warning: file_get_contents(https://finance.google.com/finance/converter?a=100&from=INR&to=BTC&meta=ei%3DZsa7WeGkE4_RuASY95SQAw): failed to open stream: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in F:\Xampp\htdocs\test\index.php on line 11

Notice: Undefined offset: 1 in F:\Xampp\htdocs\test\index.php on line 16
0

How to fix this error?

3

There are 3 answers

1
lollo On

I had the same problem. I think that google has changed way to output the result. Try this (works for me, tested today at 12.14 PM CEST (UTC+2))

  function convertCurrency($amount, $from, $to) {
     $url = 'http://finance.google.com/finance/converter?a=' . $amount . '&from=' . $from . '&to=' . $to;
     $data = file_get_contents($url);
         preg_match_all("/<span class=bld>(.*)<\/span>/", $data, $converted);
         $final = preg_replace("/[^0-9.]/", "", $converted[1][0]);
        return round($final, 3);
   }

   echo convertCurrency(1, 'EUR', 'USD');  // output: 1.195 

/* I got errors until i've changed this line:
   $final = preg_replace("/[^0-9.]/", "", $converted[1]); to:
   $final = preg_replace("/[^0-9.]/", "", $converted[1][0]);
   .. maybe it works for your code too
*/
0
Mïleñ Låzårov On

Try this
Works for me.
Change it to:

$url = "https://finance.google.com/bctzjpnsun/converter?a=$amount&from=$from_Currency&to=$to_Currency";
0
GarethWyn On

The above sort of worked for me when my script went wrong but the conversion was wrong for some reason. It seems that all I needed to do was update the URL in the script; I now have the following (I'm only converting one currency at a time but you should be able to work out how to adapt!):

function convertCurrency($to){
    $url = "http://finance.google.com/finance/converter?a=1&from=GBP&to=$to";
    // Previously: $url = "http://www.google.com/finance/converter?a=1&from=GBP&to=$to";
    $request = curl_init();
    $timeOut = 0;
    curl_setopt ($request, CURLOPT_URL, $url);
    curl_setopt ($request, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($request, CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
    curl_setopt ($request, CURLOPT_CONNECTTIMEOUT, $timeOut);
    $response = curl_exec($request);
    curl_close($request);

    $regularExpression = '#\<span class=bld\>(.+?)\<\/span\>#s';
    preg_match($regularExpression, $response, $finalData);
    $rate = $finalData[0];
    $rate = strip_tags($rate);
    $rate = substr($rate, 0, -4);
    return $rate;
}

I hope this helps.
G