Inspired by the Open Source Exchange Rates and money.js, I’ve developed a PHP class that consumes the openexchangerates.org 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 openexchangerates.org (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 (1880 downloads )
Installation
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:
require_once('../includes/openexchangerates.php');
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.
Usage
First of all, you have to decide (and understand the differences) whether to use the information provided by openexchangerates.org 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');
getBase()
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:
USD
getRate($currency)
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:
0.71312312
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
1
and irrelevant to the base,
echo $er->getRate( $er->getBase() );
should always print
1
getRates()
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
Array ( [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
Array ( [USD] => 1 )
and
$er = new ExRates('GoogleRates'); $er->getRate('EUR'); print_r( $er->getRates() );
will print something like
Array ( [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
4.3105633
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.
Hi
Thanks for this!
But shouldn’t it be: return $base / $from * $amount * $to; ?
Thanks!
/T
You are right. I’ve just released v0.2 fixing the issue.
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:
require_once(‘/includes/openexchangerates.php’);
$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!
OpenExchangeRates uses a list of currencies provided by openexhangerates.org, 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.
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’));
Actually, to answer both questions, this works perfectly for me:
and it echoes:
4.04489294329
Array
(
[USD] => 1
[EUR] => 0.799616184
[GBP] => 0.646872372
)
0.799616184
I’ll really need to know what and how exactly doesn’t work. Does something give an error message?
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!
Glad you could sort it out 🙂