Snippet. PHP. Exchange Currencies Using Yahoo API with Cache Support

The following PHP snippet successfully exchanges prices between two currencies using the online Yahoo API. The snippet also supports cache support, thus limiting unnecessary queries over the net which tend to slow the execution time. The snippet is well commented, so it can be easily updated to suit requirements.

//======================== START OF FUNCTION ==========================//
// FUNCTION: exchange_price                                            //
//=====================================================================//
function exchange_price($price,$from_currency,$to_currency,$update=43200){
	$from_currency = strtoupper(trim($from_currency));
	$to_currency = strtoupper(trim($to_currency));
	
	// Same currency? Y => Return same price
	if($from_currency == $to_currency)return $price;
	
	$exchange_rate = false;
	$exchange_rate_time_updated = false;
	$currencies = array();
	$currency_stamp = $from_currency.$to_currency;
	$exchanged_price = false;
	
	// START: Load cache
	$cache_file = 'currency_conversion_rates.dat';
	
	    // Does cache file exists? N => Create it.
	if (file_exists($cache_file) == false) {
		file_put_contents($cache_file, serialize($currencies));
	}
	    // Load file
	$data = file_get_contents($cache_file);
	if ($data != false && empty($data)!=true) {
		$currencies = unserialize($data);
	}
	
	// Does it exist in cache? Y => Load from cache
	if (isset($currencies[$currency_stamp]) == true) {
		$exchange_rate = $currencies[$currency_stamp]['rate'];
		$exchange_rate_time_updated = $currencies[$currency_stamp]['updated'];
	}
	// END: Load cache
	
	// Cache successfully loaded.
	if ($exchange_rate !==false && $exchange_rate_time_updated !== false) {
		// Is it time to update? N => Return exchanged price
		if ((time() - $exchange_rate_time_updated) < $update) {
			$exchanged_price = $price * $exchange_rate;
		    return $exchanged_price;
		}		
	}
	
	// START: Load rates from Yahoo
	$url = 'http://quote.yahoo.com/d/quotes.csv?s='.$currency_stamp.'=X&f=sl1d1t1c1ohgv&e=.csv';
	$data = file_get_contents($url);
	if ($data !== false && stripos($data, $currency_stamp) !== false) {
		$data = str_replace("\"", "", $data);
	    $data = explode(",", $data);
	    $exchange_rate = $data[1];	    
	}	
	// END: Load rates from Yahoo
	
	// START: Save to cache
	if ($exchange_rate !== false) {
		$currencies[$currency_stamp]['rate'] = $exchange_rate;
		$currencies[$currency_stamp]['updated'] = time();
		file_put_contents($cache_file, serialize($currencies));
	}
	// END: Save to cache
	
	$exchanged_price = $price*$exchange_rate;
	return $exchanged_price;
}
//=====================================================================//
//  FUNCTION: exchange_price                                           //
//========================= END OF FUNCTION ===========================//

Example usage


// Exchange between USD and EUR
$to_eur_price = exchange(2000,'USD','EUR');

// Exchange between USD and GBP
$to_gbp_price = exchange(200.00,'USD','GBP');

// Exchange between EUR and USD
$to_usd_price = exchange(30.00,'EUR','USD');


Updated on: 24 Apr 2024