ChillNet - The Power of Simplicity - Development, Hosting, Bulk SMS and Fax2email made easy!

PHP Currency Convertor Class PDF Print E-mail
Tuesday, 21 July 2009 06:41

How many times in the past have you had the need to quickly integrate a currency converter into your PHP applications.  Well at least I did, and I thought I'll share the code with the open source community.  Credits given to Google for making it easy :-)

class Currency {
/**
* Convert a given Currency to another using Google
*
* @param string $from_Currency
* @param string $to_Currency
* @param integer $amount
* @return float
*/
public static function convert($from_Currency, $to_Currency, $amount) {
$amount = urlencode($amount);
$from_Currency = urlencode($from_Currency);
$to_Currency = urlencode($to_Currency);
$url = "http://www.google.com/ig/calculator?hl=en&q=$amount$from_Currency=?$to_Currency";
$ch = curl_init();
$timeout = 0;
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);
$rawdata = curl_exec($ch);
curl_close($ch);
$data = explode("\"", $rawdata);
$data = explode(' ', $data['3']);
$var = $data['0'];
return round($var, 2);
}
}

If you'd like to convert South African Rand to US Dollers, simply do the following:

echo Currency::convert('ZAR', 'USD', 100);

 

 

Last Updated ( Thursday, 10 February 2011 17:42 )
 

Signup For Fax2Email

 
Get your free Fax2Email number
 
Email Address:
Contact Name:
Contact Number:
 
 

Random Products

R7 500.00

R110.00



Banner