3

I have the following code (below) and was using the iGoogle version.

   $url = 'http://www.google.com/ig/calculator?hl=en&q=' . $amount . $from_Currency . '=?' . $to_Currency;

    $ch = curl_init();
    $timeout = 0;

    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch,  CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $rawdata = curl_exec($ch);
    curl_close($ch);

    $data = explode('"', $rawdata);
    $data = explode(' ', $data[3]);
    $var = $data[0];

BUT having looked they are using a different URL:

  'http://www.google.com/finance/converter?hl=en&a=' . $amount . '&from=' . $from_Currency . '&to=USD';

But simply changing the url does not return the required result that i was used to.

Now all i would get is

 http://www.w3.org/TR/html4/strict.dtd

SO has anyone worked on this latest currency converter URL or have any ideas. Or a replacement using PHP

Kara
  • 6,115
  • 16
  • 50
  • 57
Simon Davies
  • 3,668
  • 9
  • 41
  • 69

4 Answers4

14

Thanks to some more in depth looking and rewording of issue found this post. So in a way its a duplicate. but here is the question:

Need API for currency converting

I used @hobailey answer for a temporary fix until i can update it to another version or google decide to do a proper api.

  $amount = urlencode($amount);
  $from_Currency = urlencode($from_Currency);
  $to_Currency = urlencode($to_Currency);
  $get = file_get_contents("https://www.google.com/finance/converter?a=$amount&from=$from_Currency&to=$to_Currency");
  $get = explode("<span class=bld>",$get);
  $get = explode("</span>",$get[1]);  
  $converted_amount = preg_replace("/[^0-9\.]/", null, $get[0]);
Community
  • 1
  • 1
Simon Davies
  • 3,668
  • 9
  • 41
  • 69
  • Hi Thanks, sorry without seeing or using woocommerce/wordpress I cannot help you but I am sure there will be help soon, or post your own question relating to your issue. sorry – Simon Davies Oct 06 '15 at 18:39
  • On my server when I use file_get_contents('https://www.google.com/finance....'), it does not return anything. So what I can do it for working? – Sanjay Goswami Jun 29 '16 at 14:03
4

Using XPath:

function currency($from, $to, $amount)
{
   $content = file_get_contents('https://www.google.com/finance/converter?a='.$amount.'&from='.$from.'&to='.$to);

   $doc = new DOMDocument;
   @$doc->loadHTML($content);
   $xpath = new DOMXpath($doc);

   $result = $xpath->query('//*[@id="currency_converter_result"]/span')->item(0)->nodeValue;

   return str_replace(' '.$to, '', $result);
}

echo currency('USD', 'EUR', 1); // returns 0.7216
ggirtsou
  • 2,050
  • 2
  • 20
  • 33
  • On my server when I use `file_get_contents('https://www.google.com/finance....')`, it does not return anything. So what I can do it for working? – Sanjay Goswami Jun 29 '16 at 14:02
  • @PlanetHackers please ask your host to enable allow_url_fopen: http://stackoverflow.com/questions/3694240/add-allow-url-fopen-to-my-php-ini-using-htaccess If that doesn't work, please check error logs to figure out what's preventing you to access that resource. It could well be that this service is not available anymore. – ggirtsou Jun 29 '16 at 19:57
2

I have created a class to connect to Google in easier way here.

currency-converter-php

I hope it should make some aspects easier!

Edit:I just knew that Google service was closed this November 2013.

I am gonna have to change it!

Edit Again: I have changed Google Api to Yahoo Api and it works perfectly fine!

Ujjwal Ojha
  • 1,340
  • 10
  • 17
  • Thanks for this nice class, will keep this in mind when i get to update the sotres. – Simon Davies Nov 07 '13 at 15:19
  • oops forgot to mention that you would need to rework on that ta little as it uses the OLD google url http://www.google.com/ig/calculator... as google now use 'https://www.google.com/finance/converter...' but its not a case of a simple url change as i thought :-) – Simon Davies Nov 07 '13 at 15:21
  • Thanks for telling. I didnt know that before! – Ujjwal Ojha Nov 07 '13 at 15:28
  • not a problem, it to a client to notice :-) Google hey! Other than the link the diff below is the explode commands as well – Simon Davies Nov 07 '13 at 16:07
  • I have changed Google Api to Yahoo Api and it works perfectly fine! – Ujjwal Ojha Nov 07 '13 at 16:08
1

I have found another solution. It will also work if your server IP is not able to use google service.

<?php
    $from_currency    = 'USD';
    $to_currency    = 'INR';
    $amount            = 1;
    $results = converCurrency($from_currency,$to_currency,$amount);
    $regularExpression     = '#\<span class=bld\>(.+?)\<\/span\>#s';
    preg_match($regularExpression, $results, $finalData);
    echo $finalData[0];

    function converCurrency($from,$to,$amount){
        $url = "http://www.google.com/finance/converter?a=$amount&from=$from&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); 
        return $response;
    } 
?>

The source reference Step Blogging

Sanjay Goswami
  • 822
  • 1
  • 16
  • 36
  • The Google Finance API was due to be be shut down on October 20th 2012. However it remained working since then, until the answer is posted. Google Finance is a discontinued service which offers no support. – Sanjay Goswami Aug 23 '19 at 13:32
  • Could you please share us some alternate solution. – Chinmay235 Aug 26 '19 at 06:40