114

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 ?

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Adam Pierce
  • 33,531
  • 22
  • 69
  • 89
  • 1
    @Kelly: Very good suggestion, because Google is free, and has great chances to still be around in the foreseeable future: I would like to suggest that you submit it as an answer. – Eric O. Lebigot Jun 05 '11 at 08:47
  • 1
    Can't seem to edit the comment. Here is the new link http://www.jarloo.com/exchange-rate-api/ – Kelly Jun 27 '12 at 14:25
  • 1
    thanks for sharing @Adam Pierce! Is there any way to get also historical rates? – Aris Mar 12 '13 at 13:34
  • You can use this link as well. Its free.Just sign up and get API KEY. http://jsonrates.com/docs/ – Gajendra K Chauhan Mar 11 '15 at 06:29
  • Use https://currencylayer.com - they provide real-time & historical rates in JSON format, along with a few PHP examples (https://currencylayer.com/documentation) – Frank May 19 '15 at 14:53
  • I am unable to understand this question, but I still think that [currency exchange](http://currencyliquidator.com/) rates can be automatically generate using simple JS. – Jaffer Wilson Oct 06 '15 at 23:34

15 Answers15

70

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

Seer
  • 5,226
  • 5
  • 33
  • 55
Greg
  • 316,276
  • 54
  • 369
  • 333
  • That is up to date and easy to parse. This is what I am after. – Adam Pierce Oct 08 '08 at 10:29
  • 4
    I am also interested in this, is there any information on what the parameters stand for? Specifically the "f" parameter. I can't find any info anywhere. – Fishcake Oct 08 '09 at 06:12
  • 10
    Is it legal to read this data if you have a commercial website? – Junior Mayhé Jan 22 '10 at 21:54
  • The "f" field seems to be a (non delimited) list of fields you wish to see in the resulting csv file. A little experimentation reveals the field code "n" may also be handy. – Myster Feb 21 '10 at 22:13
  • 4
    Further experimentation shows you can request multiple conversion rates at once by comma separating the symbols in the 's' parameter like so: http://download.finance.yahoo.com/d/quotes.csv?s=NZDEUR=X,NZDAUD=X,NZDCAD=X&f=sl1d1t1ban&e=.csv – Myster Feb 21 '10 at 22:16
  • Quick curl wrapper in Ruby: https://gist.github.com/1639522 – captainpete Jan 19 '12 at 11:36
  • Can this feed return the data in an XML format? I've had a fiddle with the query string and can't get it to do anything other than .CSV – Jamie Keeling Apr 11 '12 at 20:51
  • 3
    Is there a way to specify a date? – Maxim Egorushkin Apr 16 '12 at 08:56
  • This url comes from querying yahoo tables with YQL. There is also a rest request you can perform that will return xml. Check this link for an example: http://developer.yahoo.com/yql/console/?q=select%20%2a%20from%20yahoo.finance.xchange%20where%20pair%3D%22eurusd%2C%20gbpusd%22&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys#h=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20%28%22EURUSD%22%2C%22GBPUSD%22%29 – Marco Luglio Jun 03 '13 at 14:35
  • A quick update. s, n, l1, d1, t1, a, and b seems to stand for the yahoo xchange table columns: Symbol, Name, Rate, Date, Time, Ask and Bid – Marco Luglio Jun 03 '13 at 14:47
  • see http://stackoverflow.com/a/21627583/592868 for a php-class to retrieve currency-rates – Felix Geenen Feb 07 '14 at 12:20
  • The API has been closed now. – Gary Nov 07 '17 at 04:40
  • It doesn't work anymore – Revious Feb 13 '18 at 13:31
  • This Link does not work – Chinmay235 Apr 11 '18 at 06:27
  • Yahoo doesn't work anymore. A current option is https://swop.cx/ - allows to get rates for 180+ currencies, live and historical, in REST/JSON or GraphQL API. Disclaimer: I'm one of the Devs who created SWOP ;) – Luzian Oct 05 '20 at 17:26
41

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.

philoye
  • 2,490
  • 28
  • 23
  • Also, forex rates change all the time, and banks may only take into account the rate at midnight, local time. With ~2% daily volatility, there *is* incertitude. – Alexandre C. Sep 29 '10 at 09:02
  • 1
    That's a great point. Definitely reinforces the need for a buffer of some kind if you are displaying one currency, but charging another. For what it is worth, with my 4% padding, I've never had anyone do a chargeback. – philoye Oct 01 '10 at 06:28
  • Not all banks charge that. Credit and debit cards from Mexico, for example, will just convert the currency at the price they have that day for the currency sell, and nothing more. – Francisco Zarabozo Mar 23 '17 at 16:25
28

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).

Jacco
  • 23,534
  • 17
  • 88
  • 105
13

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!

Natsukane
  • 681
  • 1
  • 8
  • 19
13

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. ]

Community
  • 1
  • 1
zeFree
  • 2,129
  • 2
  • 31
  • 39
6

I added Open Data table to YQL, you can use it to retrieve exchange rate data from yahoo.finance.

Try it in YQL console

Comma-separated format is preferrable over "where pair in ('EURUSD','GBPUSD')" but anyway, you can use both and even intermix them.

mtelis
  • 654
  • 1
  • 6
  • 10
4

Here is a Soap service that offers exchange rate

http://www.newyorkfed.org/markets/pilotfx.html

Ólafur Waage
  • 68,817
  • 22
  • 142
  • 198
4

This site has a currency converter service for free:

http://www.webservicex.net/WS/WSDetails.aspx?WSID=10

David Wengier
  • 10,061
  • 5
  • 39
  • 43
3

iGoogle was retired on November 1, 2013. This API no longer works.

To get the exchange rate you can use something like this:

function get_exchange_rate($from, $to){
    $data = file_get_contents("http://www.google.com/ig/calculator?hl=en&q=1{$from}=?{$to}");
    preg_match('/rhs\:\s?"([0-9\.]+)/', $data, $m);
    return $m[1];
}

You could add a DB cache in there to make sure you don't get throttled etc.

As has been noted on other posts / comments you'd then use this rate to calculate your currencies

oodavid
  • 2,148
  • 2
  • 23
  • 26
3

Try this RESTful (I'm not sure if this is really a REST, since i got this originally from a SOAP, I just tried to access it using HTTP GET)

Kevin
  • 451
  • 6
  • 13
  • I don't see where you supplied the "amount" to convert, yet you get some number back... can you please explain this? – SpokaneDude Oct 03 '11 at 13:44
  • 1
    It doesn't accept an amount. This will only convert fromCurrency to toCurrency where fromCurrency's amount is always one (1). Then it's up to you to make your algorithm of converting your amount – Kevin Oct 04 '11 at 08:33
2

Oanda.com exposes currency rates as an XML API, but not for free

Eugene Osovetsky
  • 6,443
  • 2
  • 38
  • 59
2

XE.com provides feed for their exchange rates. Not free though.

jop
  • 82,837
  • 10
  • 55
  • 52
1

This is working for me .

A currency exchange rate API : http://currency-api.appspot.com/

Nirav Ranpara
  • 13,753
  • 3
  • 39
  • 54
1

coinnill.com has a sort-of web-service.

http://coinmill.com/rss/AUD_USD.xml

will give you the AUD --> USD rate for example. You'll just need to parse the XML that comes back.

jop
  • 82,837
  • 10
  • 55
  • 52
cagcowboy
  • 30,012
  • 11
  • 69
  • 93
  • That data seems out of date. It says AUD is 0.77 but I know it is 0.69 today. – Adam Pierce Oct 08 '08 at 10:12
  • Also, the useful piece of data (the exchange rates) are the only bit not encoded in XML! It's just plaintext in a CDATA... – Rikki Apr 03 '12 at 15:19
1

I feel compelled to add:

http://www.exchangerate-api.com/

Dead simple to use with a clean RESTful API and signup takes 5 seconds. Includes coding examples for most major languages, most are 2-3 lines long.

Rates are updated hourly, so it's fine for most uses, and you can get 30000 monthly queries for $7 a month. I've never needed more than that, but the rates are very reasonable for higher volumes.

Matt H
  • 256
  • 3
  • 11
Alex Recarey
  • 20,178
  • 4
  • 25
  • 22