Free Open Source Exchange Rates for PHP

Inspired by the Open Source Exchange Rates and money.js, I’ve developed a PHP class that consumes the service. Since the service fetches the exchange rates from the (unofficial) Google Calculator API, I played around with it as well, and found some differences on the exchange rates provided by the two services. It is probably that some caching is involved with (it updates every hour), but still, the rates are different, and for some applications more up-to-date rates may be necessary.

So I made both available for use with the class. You only need to instantiate the class passing the appropriate parameter on the constructor, and you are ready to start converting.

Download Open Source Exchange Rates for PHP (1340 downloads)



Installation is really simple. Once you download the zip file, extract and move openexchangerates.php to your desired directory, e.g. your /includes directory. Then just include it in your scripts. For example:


Just make sure your server supports outgoing connections, as file_get_contents() is used by the class.

Class Description

The class provides four methods you can use. Here is a rough description of each one.

  • getBase() - Returns a string that represents the currency used as the base. All other currencies are given relative to the base. For now, only U.S. Dollars (USD) are supported as the base.
  • getRate() - Returns the exchange rate for the currency passed to it, relative to the base.
  • getRates() - Returns an associative array of all in-memory currencies and their exchange rates, relative to the base.
  • change() - You pass the amount and the currencies, and works it out for you. The end.

There are a few differences that either directly affect the results you get from the functions, or affect the performance of the class, depending on the provider you choose. Specifically, when you choose OpenExchangeRates as your provider, there is only one page request when the class is instantiated, and all rates are fetched at the same time. When GoogleRates is used as the provider, a separate page request must be made for each currency. Of course, this is time consuming, so it doesn’t happen unless the rate of a currency is needed. As a result, getRates() only returns an associative array of currencies that have so far been requested. Also, since the base currency is predefined as USD (and cannot be changed for the moment), if change() is called for two other currencies that are both used for the first time, two page requests will take place.

In both cases though, since no file caching is implemented, for every object created there will be at least one page request. All in all, OpenExchangeRates is faster, but may not be as up-to-date as GoogleRates. Pick and choose according to your needs.


First of all, you have to decide (and understand the differences) whether to use the information provided by or Google itself (read Class Description above).

Creating the object

To instantiate the class with the default provider OpenExchangeRates:

$er = new ExRates();

or explicitly

$er = new ExRates('OpenExchangeRates');

and to instantiate it with GoogleRates

$er = new ExRates('GoogleRates');


This method takes no parameters and returns a string with the currency code used as the base for all rates.

When OpenExchangeRates is the provider used, it defaults to USD as this is what the service currently uses, but might change in the future. The class is flexible enough to take into account if/when the base changes and make conversions appropriately.

In the case of GoogleRates, the base is USD and currently cannot be changed.

Example use:

echo $er->getBase();

would print:




This method takes a string with a currency code as as its sole parameter, and returns its exchange rate relative to the base currency as a float. Boolean FALSE is returned if the currency or rate cannot be found.

echo $er->getRate('EUR');

would print something like:


Notice that if you try to get the rate of the base currency, it should always return 1. So, assuming USD is the base,

echo $er->getRate('USD');

should print


and irrelevant to the base,

echo $er->getRate( $er->getBase() );

should always print




This method takes no parameters, and returns an associative array with the current in-memory exchange rates that the class contains.

print_r( $er->getRates() );

will print something like

    [USD] => 1
    [AED] => 3.67300014
    [ANG] => 1.74999869
    [UYU] => 19.7499654
    [YER] => 217.864924
    [ZAR] => 7.98830512

In the case of GoogleRates

$er = new ExRates('GoogleRates');
print_r( $er->getRates() );

will print

    [USD] => 1


$er = new ExRates('GoogleRates');
print_r( $er->getRates() );

will print something like

    [USD] => 1
    [EUR] => 0.72300014


change($amount, $currency_from, $currency_to)

This method is what the whole class is really all about. It accepts three parameters. The first is a number, the amount of money you want to exchange. The second is a string, the currency that this amount is in. And the third, also a string, is the currency you want to make the conversion to. If successful, the method returns a float with the converted amount, or boolean FALSE on failure.

echo $er->change(5, 'EUR', 'GBP');

should return something like



To do

A lot of work could go into building more features or making the class more efficient in terms of page requests, caching and the such, but since it covers my needs for now, I’m leaving it as it is. Of course, if I need something more, or anyone asks me for an update/feature/bug-fix I will try to make time and work on it.

Do you like it? Does it cover your needs? Do you have any suggestions and/or comments? Please let me know in the comments section below.

You might be interested in …

WordPress loops, latests posts, sticky posts and problems


I just figured out a problem (or just a nuisance?) of WordPress 3.0.1 that was driving me crazy, but finally got it working as expected. To all you WordPress theme developers out there, I hope the following explanation of the problem and the solution will save you a few minutes or hours of head-scratching.

Read More

Setting up an environment

Speaking terminal

So, in order to have a vanilla environment where I’ll get to play around and have to configure most of the stuff myself (in order to force myself to learn), I decided to install CentOS 7 in a virtual machine. This will give the added benefit of keeping snapshots so that in case I get […]

Read More

Setting up a hostname and Avahi (mDNS)

Speaking terminal

We previously made sure our machine can access the internet. In this post, we’ll configure our machine so that we can access it from the local network, without ever knowing its IP address.

Read More


  1. Hi,

    Thanks for your script, unfortunately I need to convert from a dynamic currency, not only USD.

    With OpenExchangeRates, the page loads for 10 seconds but I never get any result (using getRates or Change).
    The code is:
    $er = new ExRates(‘OpenExchangeRates’);
    echo $er->change(5, ‘EUR’, ‘GBP’);

    Any idea what it could be?

    With Google it works fine with GetRates but the change function is not available.

    What is the limitation with Google, is it related to the code or to Google itself?

    Thanks a lot!

    1. OpenExchangeRates uses a list of currencies provided by, so when the object initializes it already knows the full list of available currencies and how their exhange rate to the base currency (USD).

      Google doesn’t provide such a list. For this reason, every rate must be fetched individually (which is a lot of page requests), and that’s the reason why the Google provider doesn’t has a list of rates. If you perform a few conversions, the internal object’s list fills up, and the getRates starts filling up.

  2. Also, with google only this code works:

    print_r( $er->getRates() );

    this one doesn’t work, it loads for a long time and doesn’t return anything :
    print_r ($er->getRate(‘EUR’));

    1. Actually, to answer both questions, this works perfectly for me:

      	$er = new ExRates('GoogleRates');
      	echo $er->change(5, 'EUR', 'GBP');
      	echo "<pre>".print_r($er->getRates(), true)."</pre>";
      	echo $er->getRate('EUR');

      and it echoes:


      [USD] => 1
      [EUR] => 0.799616184
      [GBP] => 0.646872372


      I’ll really need to know what and how exactly doesn’t work. Does something give an error message?

      1. Hi,
        Actually it’s working now! The problem was the proxy server. The connection to the website couldn’t be done.

        When taking in account the proxy server it works!

        Thanks anyway!

Leave a Reply

Your email address will not be published. Required fields are marked *