Oct
13
Posted on October 13, 2008 by Dayson Pais (Articles, PHP, WebDev)

php currency conversion

With global economic meltdowns & fluctuating prices, we often can’t rely on human entry to get up to date currency rates in our web applications. Especially if you are building a website which may require your products cost to be displayed in various currencies.

This is a very powerful and light weight PHP class that lets you convert between 65+ currencies using Google Finance (http://www.google.com/finance/converter)

Example:

CurrencyRates::Convert('USD', 'INR');

Output:

49 //Since $1 is Rs. 49

Downloads:

  1. In order to connect to Google Finance, you must have CURL library installed & setup with php on your server.
  2. In case you do not have CURL installed, try deleting the getPage($url) function and uncomment the getPage($url) function below it. This function requires ‘allow_url_fopen‘ to be enabled on your server.
  3. In case you have any further problems or in understanding, post a comment below or feel free to contact me
  4. Also, if you found this article or code useful, post your comments to guide others!
    Print Version    

Comments

Nick on 12 February, 2009 at 4:33 am #

The URL has changed to :
$url = “http://www.google.com/finance/converter?a=1&from=$from&to=$to”;


Dayson Pais on 12 February, 2009 at 4:07 pm #

Hey Nick

Thanks for updating on the new url to google finance. I have updated the code and the post with the latest working class file.

Cheers!


Miguel on 23 March, 2009 at 10:08 pm #

The script have some problems … first, the convert method did not receive any price to convert. That price need to be include in the google call.


Dayson Pais on 25 March, 2009 at 3:38 pm #

The convert method is coded for 1 unit of currency. If you wish to convert more than 1 unit, then you can multiply it outside the class.


Chris Chinchilla on 6 August, 2009 at 11:32 am #

Great, very helpful, I managed to integrate it into CakePHP as a helper easily to!


Marc Warne on 23 September, 2009 at 10:36 pm #

Fantastic bit of code - I didn’t realise that converting currencies like this could be so easy!


Dayson Pais on 23 September, 2009 at 11:22 pm #

@Marc Warne
I’m glad you liked it!


Post a Comment
Name:
Email:
Website:
Comments: