Easy FX currency converter in PHP

Written by - 0 comments

Published on June 14th 2017 - Listed in PHP

While I was programming an order form which at the end shows a price in the local currency (CHF), I also wanted to show the price in EUR and USD as a reference.

I already thought I'd have to use some very complex tasks but then I came across fixer.io, an open source exchange rate API. In the background it uses exchange rates updated and released once a day from the ECB (European Central Bank). As I don't need realtime exchange rates, that's perfect.

As my base currency is Swiss Francs (CHF), I set the base currency to CHF like this:

$ curl http://api.fixer.io/latest?base=CHF

As you can see, this results in a json output. To have the output readable for a human, one can use jshon:

$ curl -q http://api.fixer.io/latest?base=CHF |jshon
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   447  100   447    0     0   5623      0 --:--:-- --:--:-- --:--:--  5658
 "base": "CHF",
 "date": "2017-06-14",
 "rates": {
  "AUD": 1.359,
  "BGN": 1.7986,
  "BRL": 3.4051,
  "CAD": 1.3607,
  "CNY": 7.0026999999999999,
  "CZK": 24.059999999999999,
  "DKK": 6.8385999999999996,
  "GBP": 0.80889999999999995,
  "HKD": 8.0367999999999995,
  "HRK": 6.8048000000000002,
  "HUF": 281.75999999999999,
  "IDR": 13684.0,
  "ILS": 3.6362999999999999,
  "INR": 66.245000000000005,
  "JPY": 113.65000000000001,
  "KRW": 1159.0999999999999,
  "MXN": 18.584,
  "MYR": 4.3868,
  "NOK": 8.6572999999999993,
  "NZD": 1.4211,
  "PHP": 51.006999999999998,
  "PLN": 3.8593999999999999,
  "RON": 4.1993999999999998,
  "RUB": 58.805,
  "SEK": 8.9616000000000007,
  "SGD": 1.4215,
  "THB": 34.966999999999999,
  "TRY": 3.6198000000000001,
  "USD": 1.0303,
  "ZAR": 13.122999999999999,
  "EUR": 0.91961999999999999

Nice! This gets me somewhere already!

Now I needed to use this data in my PHP code. And this is actually bloody simple:

// Currency converter
$fxrates = json_decode(file_get_contents('http://api.fixer.io/latest?base=CHF'),true);
//echo '

' . print_r($fxrates, true) . '
echo $fxrates['rates']['EUR'];

First the API URL is called and loaded into PHP using file_get_contents. The json content is immediately decoded using json_decode and stored in a nested array $fxrates.
The commented line can be used for printing the whole $fxrates array (which helped me find the final variable name).
The last line finally prints/echoes the exchange rate CHF/EUR.

$ php chfeur.php

This works of course with all the other listed currencies, too.

Kudos to Hakan Ensari, who has also put fixer.io on Github.

Update July 24th 2018
So it looks like fixer.io cannot be used anymore for this. It's necessary now to sign up to use the API (and receive an access token) but more importantly they require a paid subscription if one wants to convert a currency or retrieve the exchange rates from a certain base currency. In the free plan only the EUR currency can be used as base currency (at least when I tried). So for now I've switched to http://free.currencyconverterapi.com/.

Add a comment

Show form to leave a comment

Comments (newest first)

No comments yet.