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:


Based on Google Currency Converter API, I build an PHP Function to get Currency values from the out put of as PHP Array
function google_currency_converter($amount,$to,$from="INR")
 $content = file_get_contents("$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: