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    = "$amount$from=?$to";
    $agent  = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/20080311 Firefox/";
    $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);

    $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 Altaf Hussain on 4:10 PM Rating: 5


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

  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!

  3. Nice question. The URL used above ( 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 ( does the same thing but good for manual use.

  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.



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

    1. Yeah, you are right. An alternative method to convert currency can be found here.


Altaf Web. Powered by Blogger.