Programmatically access currency exchange rates [closed]
Asked Answered
T

15

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 ?

Triplet answered 8/10, 2008 at 9:58 Comment(6)
@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.Valine
Can't seem to edit the comment. Here is the new link jarloo.com/exchange-rate-apiCellulous
thanks for sharing @Adam Pierce! Is there any way to get also historical rates?Esch
You can use this link as well. Its free.Just sign up and get API KEY. jsonrates.com/docsEvaporation
Use currencylayer.com - they provide real-time & historical rates in JSON format, along with a few PHP examples (currencylayer.com/documentation)Interblend
I am unable to understand this question, but I still think that currency exchange rates can be automatically generate using simple JS.Continuum
B
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

Baseline answered 8/10, 2008 at 10:23 Comment(15)
That is up to date and easy to parse. This is what I am after.Triplet
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.Ilion
Is it legal to read this data if you have a commercial website?Batter
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.Entrammel
Further experimentation shows you can request multiple conversion rates at once by comma separating the symbols in the 's' parameter like so: download.finance.yahoo.com/d/…Entrammel
Quick curl wrapper in Ruby: gist.github.com/1639522Emblematize
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 .CSVLaughton
Is there a way to specify a date?Cranny
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: developer.yahoo.com/yql/console/…*%20from%20yahoo.finance.xchange%20where%20pair%20in%20%28%22EURUSD%22%2C%22GBPUSD%22%29Molality
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 BidMolality
see https://mcmap.net/q/168356/-need-api-for-currency-converting-closed for a php-class to retrieve currency-ratesBootlace
The API has been closed now.Lavabo
It doesn't work anymoreChalone
This Link does not workFrederique
Yahoo doesn't work anymore. A current option is 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 ;)Nonary
A
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.

Ay answered 5/4, 2010 at 12:18 Comment(3)
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.Obadias
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.Ay
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.Host
C
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).

Corneous answered 28/2, 2009 at 16:35 Comment(3)
Just checked (20 minutes after your comment) and it available for me.Corneous
I've just been trying to use this, but VB.NET refuses to XPath it properly. Is this because the URLs defining the namespaces are both 404ing?Tillietillinger
this link always shows yesterday's rates, not today's.Lovage
C
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. ]

Cullen answered 5/12, 2011 at 20:41 Comment(3)
Perhaps its not working anymoreAceae
Thanks @dskanth for reminding. Link and information updated. : )Cullen
A free OpenExchangeRates plan is available here with 1000 requests a month .. its a tiny link so you won't spot it in most cases .. openexchangerates.org/signup/freeWashery
P
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!

Percept answered 2/10, 2012 at 2:4 Comment(4)
You can use json_decode instead of explode if you want it to be more reliable.Condole
are there limitations with this service?Dinnage
"iGoogle was retired on November 1, 2013", your app doesn't work any moreGriseldagriseldis
I used similar code which has been changed to use the Yahoo quotes.csv method with only two line changes in code.Jasso
P
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.

Package answered 25/11, 2010 at 6:44 Comment(2)
Thanks dude. This combined with the YQL docs on PHP helped me big time!Kherson
hi, can we add date parameter e.g. I want to get historical exchange rate.Yardley
N
4

Here is a Soap service that offers exchange rate

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

Nev answered 8/10, 2008 at 10:7 Comment(3)
Shame it's SOAP and not REST. Apart from that, it's a useful facilityKilometer
Is this free and stable? Will I be able to ship software that relies on this?Landri
apparently they stopped doing this @ 31.12.2008Collude
R
4

This site has a currency converter service for free:

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

Repose answered 8/10, 2008 at 10:10 Comment(0)
H
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)

Heatstroke answered 1/10, 2011 at 6:58 Comment(2)
I don't see where you supplied the "amount" to convert, yet you get some number back... can you please explain this?Gramps
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 amountHeatstroke
C
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

Columnar answered 15/8, 2012 at 11:35 Comment(0)
P
2

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

Penates answered 8/10, 2008 at 10:6 Comment(0)
F
2

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

Florez answered 1/10, 2009 at 0:20 Comment(0)
C
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.

Cruise answered 8/10, 2008 at 9:59 Comment(2)
That data seems out of date. It says AUD is 0.77 but I know it is 0.69 today.Triplet
Also, the useful piece of data (the exchange rates) are the only bit not encoded in XML! It's just plaintext in a CDATA...Tillietillinger
M
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.

Mellar answered 29/9, 2010 at 8:58 Comment(0)
S
1

This is working for me .

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

Southernly answered 22/11, 2012 at 11:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.