How to convert currency using Google API

Post Reply
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

How to convert currency using Google API

Post by Saman » Mon Jan 15, 2018 9:04 pm

Function:

Code: Select all

<?php
function currencyConverter($from_Currency,$to_Currency,$amount) {
	$from_Currency = urlencode($from_Currency);
	$to_Currency = urlencode($to_Currency);
	$get = file_get_contents("https://finance.google.com/finance/converter?a=1&from=$from_Currency&to=$to_Currency");
	$get = explode("<span class=bld>",$get);
	$get = explode("</span>",$get[1]);
	$converted_currency = preg_replace("/[^0-9\.]/", null, $get[0]);
	return $converted_currency;
}
?>
Usage:

Code: Select all

<?php
	// change amount according to your needs
	$amount =10;
	// change From Currency according to your needs
	$from_Curr =“INR”;
	// change To Currency according to your needs
	$to_Curr =“USD”;
	$converted_currency=currencyConverter($from_Curr, $to_Curr, $amount);
	// Print outout
	echo $converted_currency;
?>
Post Reply

Return to “PHP & MySQL”