Location: PHPKode > scripts > Simple PHP Currency Exchanger > simple-php-currency-exchanger/class.currencyExchange.php
<?php
/************************************************************************************************************************
* Objective : Class used to fetch exchange rate from a remote feed.
			  Feed is taked from http://www.currencysource.com/
 			  Seperate feed is available for all supported base currencies
			  Path for base currency feed is http://www.currencysource.com/rss/[baseCurrencyCOde].xml
* Author : P.Shunmuga prasath <hide@address.com>
* Created Date : 03/08/2007
* Modified By : P.Shunmuga prasath <hide@address.com>
* Modified Date : 03/08/2007
**************************************************************************************************************************/

/**
 * @category Currency Exchange
 * @package Currency Exchange
 * @author P.Shunmuga prasath <hide@address.com>
*/

class currencyExchange extends ParseXML
{
	/**
	 * Base currecny Code
	 * @var String
	 */

	var $baseCurrencyCode;

	/**
	 * Target currecny Code
	 * @var String
	 */
	var $targetCurrencyValue;

	/**
 	* Constructor function for the class
 	* @param Integer $baseCurrencyCode Base currency code.
 	* @return void
 	*/
	function currencyExchange($baseCurrencyCode, $targetCurrencyValue)
	{
		$this->baseCurrencyCode = $baseCurrencyCode;	// Assign base currency code to a private variable
		$this->targetCurrencyValue = $targetCurrencyValue;	// Assign target currency value to a private variable
	}

	/**
 	* Calculate the exchange rate for a given base and target currency from a remote RSS feed
 	* @return float
 	*/
	function getExchangeRate()
	{
		$baseCurrencyCode = $this->baseCurrencyCode;	// Assign base currency code
		$targetCurrencyValue = $this->targetCurrencyValue;	// Assign target currency value
		$baseFolder = "./";
		if($this->getXmlArray()) {	// Parse xml file and create an array
			$parseXml = new ParseXML();
			$today = mktime(0, 0, 0, date("m")  , date("d"), date("Y"));
			$file = $baseFolder.strtoupper($baseCurrencyCode) . "_" . $today .".xml";
			if(file_exists($file))
				$exchangeRateArray = $parseXml->GetXMLTree($file);
			else
				echo '<center><font color="#FF0000"><b>XML File not Found</b></font></center>';
			$rateStr = $exchangeRateArray["rss"][0]["channel"][0]["item"][$targetCurrencyValue]["title"][0]["value"];	// Get the rate with base and target currency code
			// Parse and retreive the exchange rate from the rate string and return it
			$start = strpos($rateStr, "(")+1;
			$stop = strpos($rateStr, ")") - $start;
			$rate = substr($rateStr, $start, $stop);
			return $rate;
		}
		else
			return print  '<font color="#FF0000"><b>An Error Occured in fetching the Exchange Rate</b></font>';
	}

	/**
 	* parse remote feed and create a local copy
 	* @return boolean
 	*/
	function getXmlArray()
	{
		$baseCurrencyCode = $this->baseCurrencyCode;	// Assign base currency code
		$today = mktime(0, 0, 0, date("m")  , date("d"), date("Y"));	// Get today's timestamp to check the xml file existence
		$yesterday = mktime(0, 0, 0, date("m")  , date("d")-1, date("Y"));	// Get yesterday's timestamp to delete old files
		$url = "http://www.currencysource.com/rss/" . strtoupper($baseCurrencyCode) .".xml";	// Assign feed url
		$baseFolder = "./";	// Folder in which the local copy of feed is to be created
		chmod($baseFolder, 0777);
		$file = $baseFolder.strtoupper($baseCurrencyCode) . "_" . $today .".xml";
		if(!file_exists($file)) {	// If file does not exists, parse remote feed and create a local copy
			$strToWrite = file_get_contents($url);	// open or create a new xml file
			$handle = fopen($file, "w+");
			chmod($file, 0777);
			fwrite($handle, $strToWrite);	// Write feed contents to xml file
			if(file_exists($baseFolder.strtoupper($baseCurrencyCode) . "_" . $yesterday .".xml"))	// If old file exists delete it
				unlink($baseFolder.strtoupper($baseCurrencyCode) . "_" . $yesterday .".xml");
			return true;
		}
		else if(file_exists($file))
			return true;
		else
			return false;
	}
}

?>
Return current item: Simple PHP Currency Exchanger