Programmatically access currency exchange rates [closed]
I'm setting up an online ordering system but I'm in Australia and for international customers I'd like to show prices in US dollars or Euros so they don't have to make the mental effort to convert from Australian dollars.
Does anyone know if I can pull up to date exchange rates off the net somewhere in an easy-to-parse format I can access from my PHP script ?
UPDATE: I have now written a PHP class which implements this. You can get the code from my website.
You can get currency conversions in a simple format from yahoo:
For example, to convert from GBP to EUR:
http://download.finance.yahoo.com/d/quotes.csv?s=GBPEUR=X&f=sl1d1t1ba&e=.csv
This answer is VERY late, but there is a key bit of info missing from the above answers.
If you want to show accurate prices to your customers it is important to understand how foreign exchange rates work.
Most FX services only quote the spot rate (midway between the Bid and Ask). The spot is a kind of shorthand for the exchange rate, but no one gets the spot because you can only sell at the bid or buy at the ask. You're usually looking at least a 1% spread between them, so the spot rate is 0.5% off for your customers.
But it doesn't stop there, your customers almost certainly are using a credit card and Visa/Mastercard/Amex all charge foreign exchange fees. These are non-trivial in my experience, at LEAST 2.5%. For example, Citibank Australia charges 3.3%. These vary from card to card so there's no way for you to predict the final price that your customers will be billed.
If you want to quote an "accurate" price to your customers based on an exchange rate, you need to factor in the above and provide a buffer so that you don't end up charging more than what you quoted.
FWIW, I've been adding 4% to what the F/X conversion would otherwise indicate.
Might be nice to add
http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml
to the list.
The official reference rates provides by the European Central Bank based on the regular daily concertation procedure between central banks within and outside the European System of Central Banks.
The feed is in XML and some other formats.
Updating normally takes place at 2.15 p.m. (14:15) ECB time (= Frankfurt time).
I recently implemented the same thing, but using Google's API. The query URL looks like this:
http://www.google.com/ig/calculator?hl=en&q=1GBP=?USD
It takes 3 parameters. The first parameter is the amount, followed by the ISO 4217 currency code you're converting from, an equals sign and a question mark, and the currency code you're converting to. You can find a list of codes that Google supports here. The response to the query will look like this:
{lhs: "1 British pound",rhs: "1.6132 U.S. dollars",error: "",icc: true}
This is pretty self-explanatory, so I won't go into details here. This is how I handled the query response:
function convert_currency($amount, $from_code, $to_code){
ini_set('max_execution_time', 60);
$temp = 'http://www.google.com/ig/calculator?hl=en&q=' . $amount . $from_code . '=?' . $to_code;
$response = file_get_contents($temp);
$result_string = explode('"', $response);
$final_result = $result_string['3'];
$float_result = preg_replace("/[^0-9\.]/", '', $full_result);
return $float_result;
}
I'm sure it's far from the most elegant way to do this, but I'm pretty new to PHP. Hope it helps!
another very great free and opensource link is this:
https://raw.github.com/currencybot/open-exchange-rates/master/latest.json
(I found about it here: http://josscrowcroft.github.com/open-exchange-rates/)
[Update]:
Open Exchange Rates project data has been moved away from GitHub.
It is available now at: http://openexchangerates.org/
Data in JSON format is available at: http://openexchangerates.org/latest.json
No access fees, no rate limits, No ugly XML - just free, hourly updated exchange rates in JSON format.
This is not "entirely" free now. The new licensing states that up to 1000 hits per month is allowed, and then you need to pay. You also need to pay if you want to use the single currency converter (basic functionality).
[ Note: You may want to look at this answer as well. ]