Friday, September 28, 2012

PHP Currency Converter with Google Currency Converter API


Are you looking for a reliable PHP Currency Converter, here is Google give you a solution from their open Google Currency Converter API


How this API Works:


This PHP Currency Converter work with following URL call back.



Querying Converter API from PHP function as per following URL:


q=1USD=?INR

http://www.google.com/ig/calculator?q=1USD=?INR

Based on Google Currency Converter API, I build an PHP Function to get Currency values from the out put of http://www.google.com/ig/calculator?hl=en&q=1USD=?INR. as PHP Array
function google_currency_converter($amount,$to,$from="INR")
{
 $content = file_get_contents("http://www.google.com/ig/calculator?hl=en&q=$amount."."$from=?$to"); 
 // Remove Spaces on API Output
 $content = preg_replace('/[^A-Za-z0-9.]+/','',$content);
 // Match All Currency values 0-9 and .(decimal)
 preg_match_all('/([0-9.]+)/', $content, $m);
 
 // Convert values with 3 decimal points
 $currency[$from] = sprintf("%.3f",$m[0][0]);
 $currency[$to] = sprintf("%.3f",$m[0][1]);
 
 return $currency;
}


$c = google_currency_converter(2800,"USD");

echo "INR:".$c['INR'];
echo "<br />";
echo "USD:".$c['USD'];



1 comment: