Currency Converter Using Google Finance

Currency Converter Using Google Finance, Currency Converter Using PHP, Currency Converter code PHP

/**
* @abstract To covert the currency from one country to another
* @param $currencyFrom
* @param $currencyTo
* @param $amount
* @return $convertedCurrency
*/
function convertCurrency( $currencyFrom, $currencyTo, $amount )
{
// Encoding the string
$amount = urlencode($amount);
$currencyFrom = urlencode($currencyFrom);
$currencyTo = urlencode($currencyTo);

// Passing the given values to the url
$url = "http://www.google.com/finance/converter?a=$amount&from=$currencyFrom&to=$currencyTo";

// Initializing a cURL session
$ch = curl_init();
$timeout = 0;

// Setting an option for a cURL transfer
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);

// Performing a cURL session
$rawdata = curl_exec($ch);

// Closing a cURL session
curl_close($ch);

// Split the data with string bld>
$data = explode('bld>', $rawdata);
$data = explode($currencyTo, $data[1]);

// rounding the converted floating point value
$convertedCurrency = round($data[0], 2);

return $convertedCurrency;
}

Post a Comment

Analytics