free hit counter

Currency Conversion Using Google Calculator API

Currency Conversion Using Google Calculator API

Currency Conversion Using Google Calculator API

 

Google is a magical search engine. Google also provides many APIs to be incorporated in your application. Today, we will use Google’s hidden currency conversion API. Why hidden? Because there is no official documentation for it.

We will build a currency converter from scratch, it pulls data from Google and display the results using PHP function.

  1. function currency($from_Currency,$to_Currency,$amount) {  
  2.     $amount = urlencode($amount);  
  3.     $from_Currency = urlencode($from_Currency);  
  4.     $to_Currency = urlencode($to_Currency);  
  5.     $url = “http://www.google.com/ig/calculator?hl=en&q=$amount$from_Currency=?$to_Currency”;  
  6.     $ch = curl_init();  
  7.     $timeout = 0;  
  8.     curl_setopt ($ch, CURLOPT_URL, $url);  
  9.     curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);  
  10.     curl_setopt($ch,  CURLOPT_USERAGENT , “Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)”);  
  11.     curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);  
  12.     $rawdata = curl_exec($ch);  
  13.     curl_close($ch);  
  14.     $data = explode(‘”‘$rawdata);  
  15.     $data = explode(‘ ‘$data[‘3’]);  
  16.     $var = $data[‘0’];  
  17.     return round($var,3);  
  18. }  

echo currency(‘USD’,’INR’,1) ;

Now enjoy 🙂