Easy FX currency converter in PHP

Written by - 0 comments

Published on - 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
{"base":"CHF","date":"2017-06-14","rates":{"AUD":1.359,"BGN":1.7986,"BRL":3.4051,"CAD":1.3607,"CNY":7.0027,"CZK":24.06,"DKK":6.8386,"GBP":0.8089,"HKD":8.0368,"HRK":6.8048,"HUF":281.76,"IDR":13684.0,"ILS":3.6363,"INR":66.245,"JPY":113.65,"KRW":1159.1,"MXN":18.584,"MYR":4.3868,"NOK":8.6573,"NZD":1.4211,"PHP":51.007,"PLN":3.8594,"RON":4.1994,"RUB":58.805,"SEK":8.9616,"SGD":1.4215,"THB":34.967,"TRY":3.6198,"USD":1.0303,"ZAR":13.123,"EUR":0.91962}}

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
0.91962

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.

RSS feed

Blog Tags:

  AWS   Android   Ansible   Apache   Apple   Atlassian   BSD   Backup   Bash   Bluecoat   CMS   Chef   Cloud   Coding   Consul   Containers   CouchDB   DB   DNS   Database   Databases   Docker   ELK   Elasticsearch   Filebeat   FreeBSD   Galera   Git   GlusterFS   Grafana   Graphics   HAProxy   HTML   Hacks   Hardware   Icinga   Influx   Internet   Java   KVM   Kibana   Kodi   Kubernetes   LVM   LXC   Linux   Logstash   Mac   Macintosh   Mail   MariaDB   Minio   MongoDB   Monitoring   Multimedia   MySQL   NFS   Nagios   Network   Nginx   OSSEC   OTRS   Office   PGSQL   PHP   Perl   Personal   PostgreSQL   Postgres   PowerDNS   Proxmox   Proxy   Python   Rancher   Rant   Redis   Roundcube   SSL   Samba   Seafile   Security   Shell   SmartOS   Solaris   Surveillance   Systemd   TLS   Tomcat   Ubuntu   Unix   VMWare   VMware   Varnish   Virtualization   Windows   Wireless   Wordpress   Wyse   ZFS   Zoneminder