Here's a fuller version using text file instead of database. This version also gets its data via xml from the European Central Bank - probably a more robust approach than scraping html from Yahoo.
Code lacks any kind of error trapping - recommend you add some.
Save the code below as [path]/converter.php
Enjoy.
Kevin
www.kenthouse.com
Code:
<?php
/*
File: Currency exchange rate updater
Author: Kevin Holdridge, wwww.kenthouse.com
Last modified: 11 May 2006
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
included GNU General Public License for more details. If you
received this file without the gpl.txt file, you may view it at:
http://www.gnu.org/copyleft/gpl.html
*/
/*
-----------------------------
Requirements & instructions:
-----------------------------
- Need PHP4.1+
- Must be able to set cron jobs
- Must be able to set 777 permissions on a text file to allow server to update the file holding the rates
- If you don't have PHP available on commandline, you'll need to alter the cron job to use CURL or another way of simulating http
- create empty text file to hold the rates data, and set permissions, e.g.:
touch /tmp/rateslist.php
chmod 0777 /tmp/rateslist.php
- Configure and save this file as [path]/converter.php
- Sample cron entry to update the rates daily:
06 00 * * * /usr/local/bin/php [path]/converter.php
- The first time this script runs, it will populate the local file with a list of currencies and rates
- Include the following code near the top of any file that displays price data in order to get the rate or rates into an array ($conversion_rates)
<?php
$conversion_rates = array();
$handle = @fopen($ratesfile, "r");
if ($handle)
{
while (!feof($handle))
{
$buffer = fgets($handle, 4096);
$pieces = explode(":", $buffer);
$key = $pieces[0];
$conversion_rates[$key]['label'] = $pieces[1];
$conversion_rates[$key]['rate'] = $pieces[2];
}
fclose($handle);
}
function docurconversion($val)
{
global $conversion_rates;
foreach($conversion_rates AS $k=>$v)
{
if($k)
{
$newfigure = number_format($val * $v['rate'], 2, '.', ',');
echo "{$v['label']}{$newfigure} ";
}
}
}
?>
- You can then convert or add occurrences of a price in your base currency to one or more alternative currencies
- e.g.
instead of
<?php
$a_figure = 5.50;
echo "£($a_figure}";
?>
use
<?php
$a_figure = 5.50;
docurconversion($a_figure);
?>
*/
/*
--------------------
CONFIGURATION BLOCK
--------------------
*/
//Location of remote xml file ... uisng European Central Bank in this case
$sourceurl="http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml";
//Array of alternative currencies to cover (include currency code and html entity for currency label)
//... open the above xml file to get a list of supported currencies and their codes
$currencies = array(
'GBP' => '£',
'USD' => 'US$',
'AUD' => 'AUS$',
'CAD' => 'Canadian$',
'EUR' => '€'
);
//Your base currency ... use proper currency code (see above, must exist in remote xml file)
$base_currency = 'GBP';
//Adjustment factor ... you pay more than the bank's advertised rate to convert money, so use this factor to load the conversion
//e.g. '1' means no adjustment, '1.02' adds 2% to the calculation, '1.1' adds 10%
$adjuster = 1.02;
//path and filename of local file holding the rates
$ratesfile = '/tmp/rateslist.php';
/*
--------------------------------
NO CONFIGURATION NECESSARY BELOW
--------------------------------
*/
//Load source file
$sourcedata = @file_get_contents($sourceurl);
//parse remote xml file, if currency is on our list write currency code,label, and raw exchange rate (based on Euro) into array
$rates_array = array();
$p = xml_parser_create();
xml_parse_into_struct($p, $sourcedata, $vals, $index);
xml_parser_free($p);
$rates_array['EUR'] = array(
'label' => '€',
'rate' => 1
);
foreach ($vals as $k => $v)
{
if (array_key_exists('attributes', $v))
{
if(array_key_exists($v['attributes']['CURRENCY'], $currencies))
{
$curcode = $v['attributes']['CURRENCY'];
$curlabel = $currencies[$curcode];
$currate = $v['attributes']['RATE'];
$rates_array[$curcode] = array(
'label' => $curlabel,
'rate' => $currate
);
}
}
}
// get a factor to rework all the rates based on our base currency, not on Euro
$factor = $rates_array[$base_currency]['rate'];
// modify the rates based on the base currency
foreach(array_keys($rates_array) as $k)
{
$rates_array[$k]['rate'] = ($rates_array[$k]['rate'] / $factor) * $adjuster;
}
//write currency code, label and exchange rate into local file
$fp=fopen($ratesfile,"w");
foreach ($rates_array as $k => $v)
{
fputs($fp, "{$k}:{$v['label']}:{$v['rate']}\n");
}
fclose($fp);
?>
Bookmarks