1

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?

Bip Inn
  • 31
  • 2
  • 10
  • Your missing one value while defining `$get`. I have posted my answer with refined code. – Mohammed Akhtar Zuberi Sep 15 '17 at 12:46
  • 1
    Google finance converter api is not working for some of the currency codes. refer to this: https://finance.google.com/finance/converter bitcoin is not working here too. so using php function to convert this will be useful. – SNG Sep 15 '17 at 12:48
  • Yes @sucharithagidla, we have to use finance.google.com and i am using that too. My code also works great for USD to BTC conversion but it gives that error when i try to convert INR to BTC – Bip Inn Sep 15 '17 at 13:00
  • Yesterday itself we are getting error. Let me know the solution – sankar muniyappa Mar 21 '18 at 06:21

3 Answers3

2

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

GarethWyn
  • 51
  • 1
  • 6
1

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
*/
lollo
  • 165
  • 1
  • 13
  • The conversion between the USD and EUR is working fine in my snippet too. But it does not convert the INR to BTC and vice-versa – Bip Inn Oct 06 '17 at 04:38
0

Try this
Works for me.
Change it to:

$url = "https://finance.google.com/bctzjpnsun/converter?a=$amount&from=$from_Currency&to=$to_Currency";
Rohan Pillai
  • 917
  • 3
  • 17
  • 26