Coder Perfect

Access currency exchange rates via programming [closed]

Problem

I’m setting up an online ordering system, however I’m in Australia, and I’d like to display costs in US dollars or Euros for overseas consumers so they don’t have to convert from Australian dollars.

Is there any way to get up-to-date exchange rates from the internet in an easy-to-parse manner that I can use in my PHP script?

UPDATE: I’ve built a PHP class that accomplishes this. My website has the code for you.

Asked by Adam Pierce

Solution #1

Yahoo provides currency conversions in a straightforward format:

For example, to convert from GBP to EUR: http://download.finance.yahoo.com/d/quotes.csv?s=GBPEUR=X&f=sl1d1t1ba&e=.csv

Answered by Greg

Solution #2

This response is quite late, but there is a critical piece of information missing from the other responses.

It’s critical to understand how foreign exchange rates function if you want to provide your customers realistic prices.

The spot rate is quoted by the majority of FX services (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.

However, your consumers are almost definitely using a credit card, and Visa, MasterCard, and Amex all impose foreign exchange costs. In my experience, these are at LEAST 2.5 percent non-trivial. Citibank Australia, for example, charges 3.3 percent. These differ from card to card, so there’s no way for you to know what your consumers will be charged in the end.

If you wish to quote your consumers a “correct” pricing based on an exchange rate, you must factor in the above and create a buffer so that you don’t charge more than you advertised.

To be clear, I’ve been adding 4% to what the F/X conversion would normally show.

Answered by philoye

Solution #3

It might be useful to include.

  http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml

to the list.

The European Central Bank publishes official reference rates based on a daily concertation process involving central banks inside and outside the European System of Central Banks.

The feed is available in XML and other formats. ECB time (= Frankfurt time) is generally updated at 2.15 p.m. (14:15).

Answered by Jacco

Solution #4

I did something similar lately, but with Google’s API. The query URL is as follows:

http://www.google.com/ig/calculator?hl=en&q=1GBP=?USD

It requires three parameters. The amount is the first parameter, followed by the ISO 4217 currency code you’re converting from, a question mark, and the currency code you’re converting to. A list of Google-supported codes can be found here. The following is the response to the query:

{lhs: "1 British pound",rhs: "1.6132 U.S. dollars",error: "",icc: true}

I won’t go into detail because this is very self-explanatory. This is how I responded to the query:

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 not the most elegant solution, but I’m still learning PHP. I hope this information is useful.

Answered by Natsukane

Solution #5

This is another excellent free and opensource resource:

https://raw.github.com/currencybot/open-exchange-rates/master/latest.json (I discovered it at http://josscrowcroft.github.com/open-exchange-rates/.) [Update]: The data for the Open Exchange Rates project has been moved off of GitHub. It’s now available at http://openexchangerates.org/ Data in JSON format may be found at http://openexchangerates.org/latest.json.

There are no access fees, rate limitations, or ugly XML to deal with – simply free, hourly updated exchange rates in JSON format. This is no longer “completely” free. Up to 1000 hits per month are allowed under the new licensing, after which you must pay. If you wish to utilize the single currency converter, you must also pay (basic functionality).

[Note: You might also want to look at this answer.]

Answered by zeFree

Post is based on https://stackoverflow.com/questions/181990/programmatically-access-currency-exchange-rates