Programmatically access currency exchange rates

PhpCurrencyFinance

Php Problem Overview


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.

Php Solutions


Solution 1 - Php

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

Solution 2 - Php

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.

Solution 3 - Php

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

Solution 4 - Php

another very great free and opensource link is this:

https://raw.github.com/currencybot/open-exchange-rates/master/latest.json<br> (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. ]

Solution 5 - Php

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!

Solution 6 - Php

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.

Solution 7 - Php

Here is a Soap service that offers exchange rate

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

Solution 8 - Php

This site has a currency converter service for free:

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

Solution 9 - Php

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)

Solution 10 - Php

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

Solution 11 - Php

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

Solution 12 - Php

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

Solution 13 - Php

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.

Solution 14 - Php

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.

Solution 15 - Php

This is working for me .

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdam PierceView Question on Stackoverflow
Solution 1 - PhpGregView Answer on Stackoverflow
Solution 2 - PhpphiloyeView Answer on Stackoverflow
Solution 3 - PhpJaccoView Answer on Stackoverflow
Solution 4 - PhpzeFreeView Answer on Stackoverflow
Solution 5 - PhpNatsukaneView Answer on Stackoverflow
Solution 6 - PhpmtelisView Answer on Stackoverflow
Solution 7 - PhpÓlafur WaageView Answer on Stackoverflow
Solution 8 - PhpDavid WengierView Answer on Stackoverflow
Solution 9 - PhpKevinView Answer on Stackoverflow
Solution 10 - PhpoodavidView Answer on Stackoverflow
Solution 11 - PhpjopView Answer on Stackoverflow
Solution 12 - PhpEugene OsovetskyView Answer on Stackoverflow
Solution 13 - PhpcagcowboyView Answer on Stackoverflow
Solution 14 - PhpAlex RecareyView Answer on Stackoverflow
Solution 15 - PhpNirav RanparaView Answer on Stackoverflow