Currency conversion using php and Google calculator api

Update 2:

As of November 1 2013, Google has discontinued iGoogle and hence this method no longer works. Please use the following free API instead :

Also check out our online currency conversion tool that uses the aforementioned JSON API :

While most of us use Google for searching the web for information , few are aware that it has an inbuilt calculator which can also be used for converting a currency into another . Doing so is really simple , you just type ” AMOUNT CURRENCY_CONVERTING_FROM in CURRENCY_CONVERTING_TO ” and search , the inbuilt calculator will give you the result .For example , if one wants to convert 1 U.S Dollar into Euro , he shall type “1 USD in EUR” .

Now lets get to the topic of this post , Google also has a secret calculator API ( ) that is usually used for iGoogle gadgets , but since its free and open , anyone can use it .

So if you want to convert a currency into another using the API , your callback URL would look like :

// 1 USD in EUR example

In the above example , you get a string which looks like "{lhs: "1 U.S. dollar",rhs: "0.838574423 Euros",error: "",icc: true}" . You can easily explode() the string using php and extract the required data .

To make things easier for you , here is a simple php function that converts a currency into another using the Google calculator API . This example uses cURL , so you have to have it enabled for this code to work .


function currency($from_Currency,$to_Currency,$amount) {
$amount = urlencode($amount);
$from_Currency = urlencode($from_Currency);
$to_Currency = urlencode($to_Currency);
$url = "$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);
$data = explode('"', $rawdata);
$data = explode(' ', $data['3']);
$var = $data['0'];
return round($var,3);


The value returned by Google usually has 9 decimal places , you can round it off to a no. of digits after decimal of you choice using the round() function . By default the above function rounds the value to 3 digits after decimal .

Now here is how you can use the function in your scripts


//This will display current value of 1 USD in INR ( Indian Rupees )
echo currency("USD","INR",1); 


Hope this has helped you with converting currencies using php on your website
You can view a working example of the above code at Malegaon Online .

2 responses to this entry

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

2020 All rights reserved | Powered by WordPress
Back to top
Theme by