Login   Register  
PHP Classes
elePHPant
Icontem

File: README

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Helmut Daschnigrum  >  Currency Exchange  >  README  >  Download  
File: README
Role: Documentation
Content type: text/plain
Description: Documentation
Class: Currency Exchange
Obtains exchange rates for various currencies
Author: By
Last change:
Date: 8 years ago
Size: 4,034 bytes
 

Contents

Class file image Download
CurrencyExchange class
Version 1.0.0
Copyright 2004-2005, Steve Blinch
http://code.blitzaffe.com

Returns a list of exchange rates for various currencies.


WARNING

The values and dates provided by this script are believed to be reliable,
but the author makes no warranties regarding this code, the values it
provides, or their fitness for a particular purpose, accuracy or
availability.

Note that this script relies entirely on several third-party websites
(such as the Federal Reserve Bank of New York, the Bank of Canada, and
so-on) to provide exchange rate information; if those sites become
unreachable for any reason, this script will be unable to obtain exchange
rate information.

It is *strongly* recommended that you use the values returned by this
script for informational purposes only (to provide an estimate to your
customers).  You *SHOULD NOT* rely on this script for unattended financial
transactions, as there are several situations (all of which are beyond the
control of this script) which could result in incorrect exchange rates.



EXAMPLE

// CurrencyExchange class usage example
require_once('class_CurrencyExchange.php');
$exchange = new CurrencyExchange();
if ($exchange->retrieve()) {

// Display the current Canadian exchange rate information
var_dump($exchange->rates["CAD"]);

// Display all exchange rates returned by the CurrencyExchange class
foreach ($exchange->rates as $currency=>$rate) {
echo "Currency: $currency<br>".
"Exchange rate: ".	$rate["rate"]."<br>".
"Country: ".		$rate["unit"]."<br>".
"Units: ".			$rate["unit"]."<br>".
"Date retrieved: ".	$rate["date"]."<br><br>";
}

// Display information about the source of the exchange rate information
// (eg: if it was retrieved from the Federal Reserve Bank of NY, the
// Bank of Canada, etc.)
var_dump($exchange->source);
}
// ------------------------------------------------------------------------



CACHING

It is strongly recommended that you cache the exchange rates returned by this
script to avoid overloading the source servers (and to reduce the probability
of a failure due to a temporary source server outage).

The CurrencyExchange class has a built-in serializing system that determines
whether or not the cached data is expired.  All you have to do is store the
data in between requests, and provide it to the CurrencyExchange class on the
next load.  For example:

// ------------------------------------------------------------------------

// You need to implement the load_cached_rates() function, which should retrieve
// the cached rates (from a file, or database, etc.)
$cached_rates = load_cached_rates();

$exchange = new CurrencyExchange();

// Use the set_cached() method to pass the cached data back to the class.
$exchange->set_cached($cached_rates);

// The retrieve() method will automatically detect whether or not the
// rates in $cached_rates are expired; if so, it will retrieve new rates,
// otherwise it will reuse the cached rates (and not contact the server).
if ($exchange->retrieve()) {

// Display the current Canadian exchange rate information
var_dump($exchange->rates["CAD"]);

// Display all exchange rates returned by the CurrencyExchange class
foreach ($exchange->rates as $currency=>$rate) {
echo "Currency: $currency<br>".
"Exchange rate: ".	$rate["rate"]."<br>".
"Country: ".		$rate["unit"]."<br>".
"Units: ".			$rate["unit"]."<br>".
"Date retrieved: ".	$rate["date"]."<br><br>";
}

// Display information about the source of the exchange rate information
// (eg: if it was retrieved from the Federal Reserve Bank of NY, the
// Bank of Canada, etc.)
var_dump($exchange->source);

// Use the get_cached() method to retrieve a string containing the
// exchange rate information encoded for caching.
$new_cached_rates = $exchange->get_cached();

// You need to implement the save_cached_rates() function, which should
// store the cached rates (to a file, or database, etc.)
save_cached_rates($new_cached_rates);
}
// ------------------------------------------------------------------------