Currency converter in php using google api

This api will no longer work as google has closed its iGoogle service from Nov 1, 2013. You may find this alternative method to Convert currency from one format to another.
Hello friends. Here is the google api code to convert the currency from one format to another. You have to pass just three parameteres. i.e. From currency, To currency and the amount to be converted. It will return the converted amount.


    
   function convertCurrency($from,$to,$amount) {
    $amount = urlencode($amount);
    $from   = urlencode($from);
    $to     = urlencode($to);
    $url    = "http://www.google.com/ig/calculator?hl=en&q=$amount$from=?$to";
    $agent  = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13";
    
    $ch     = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 10);
    
    $rawdata = curl_exec($ch);
    curl_close($ch);

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


Call function
echo convertCurrency('USD','INR',1);
Currency converter in php using google api Currency converter in php using google api Reviewed by JS Pixels on July 18, 2011 Rating: 5

6 comments:

  1. Thank you so much ... very useful!

    ReplyDelete
  2. One question - I've seen example code for Google's currency converter but one calls the URL in this example and one another google URL(something on the lines of /finance/calculator/
    Which one is best???

    Thanks for this by the way!

    ReplyDelete
  3. Nice question. The URL used above (http://www.google.com/ig/calculator?hl=en&q=1USD=?INR) is an api which returns JSON object as response and can be used in scripts easily. And the second URL you are talking about perhaps (http://www.google.com/finance/converter) does the same thing but good for manual use.

    ReplyDelete
  4. please send me
    the j query in which he select the option in select menu
    and the popup come out and the pop up in another file call by i frame or another location.

    thanks

    deependra

    ReplyDelete
  5. This will not work after 1 Nov, 2013. Google has closed iGoogle services.

    ReplyDelete
    Replies
    1. Yeah, you are right. An alternative method to convert currency can be found here. http://altafphp.blogspot.in/2013/11/convert-currency-using-google-finance.html

      Delete

Altaf Web. Powered by Blogger.