PHP Classes
elePHPant
Icontem

Currency Exchange: Obtains exchange rates for various currencies

Recommend this page to a friend!
  Info   View files View files (4)   DownloadInstall with Composer Download .zip   Reputation   Support forum (2)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2006-05-15 (10 years ago) RSS 2.0 feedNot enough user ratingsTotal: 1,627 All time: 2,341 This week: 897Up
Version License Categories
currencyexchanger 1.0GNU General Publi...Web services, Finances
Description Author

This class can be used to obtain exchange rates for various currencies.

It accesses remote Web sites of banks and entities that provide currency exchange rate information and parses the site page data to obtain the name, country and exchange rate for each of the supported currencies.

Currently it can obtain information about 21 currencies from the Federal Reserve Bank of New York and the Bank of Canada.

The class can cache previously obtained currency exchange information to avoid remote site access overhead in subsequent queries.

Name: Helmut Daschnigrum <contact>
Classes: 6 packages by
Country: Canada Canada
Age: ???
All time rank: 1007 in Canada Canada
Week rank: 644 Down15 in Canada Canada Down
Innovation award
Innovation award
Nominee: 1x

Details
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);
}
// ------------------------------------------------------------------------

  Files folder image Files  
File Role Description
Plain text file class_CurrencyExchange.php Class Main class file
Accessible without login Plain text file example.php Example Example script
Accessible without login Plain text file LICENSE Lic. License terms
Accessible without login Plain text file README Doc. Documentation

 Version Control Unique User Downloads Download Rankings  
 0%
Total:1,627
This week:0
All time:2,341
This week:897Up
User Comments (1)