function getRateFor($currency) {
//Load the reference rates published by the European Central Bank
$sXML = simplexml_load_file("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml");
//Find the currency rates we want
foreach($sXML->Cube->Cube->Cube as $rate)
{
if((string) $rate['currency'] == "USD") $USD = (string) $rate['rate'];
if((string) $rate['currency'] == $currency) $CUR = (string) $rate['rate'];
}
return $CUR/$USD;
}
//Use Example:
$PriceInDollars = 100;
$PriceInPounds = getRateFor("GBP")*$PriceInDollars
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (1)
Commented:
http://www.bankofcanada.ca/stats/assets/csv/fx-seven-day.csv
http://www.bankofcanada.ca/stats/assets/xml/noon-five-day.xml
I found it was a good idea to run a daily cron job to download the table into a local sql database.
That way my website is not impacted by any delays or outages at the upstream.
see
http://www.bankofcanada.ca/rates/exchange/noon-rates-5-day/
for more details and Terms & Conditions.