Tuesday, July 24, 2007

Currency conversion in PHP using Google currency conversion

Found this which is interesting and helpful to me. As i'm using it on my website that I was creating lately.

Usually if we want to retrive currency rate every day, we either
1. create a database to store the currency rate
2. using currency feed from 3rd party - which always need $$
3. using google currency conversion - free~~:)

Free things are always good. Seem's I'm lazy to update the currency rate database everyday, and I don't think my boss will pay for a latest updated currency feed, I decided to go for the google currency conversion.

Lucky I found this site - Currency conversion in PHP by Tudor . Cool ~ It is a script to call search on google currency to run a search on the google.

But I still to need so it cater for my requirements - to round up the figure without decimal points. Due to the result it return from the function is a string variable, not a number. And there is extra space in between. I need to remove the space and round up the number. Here are the codes extra I had put in.

File: exchange_rate.php
Line: the line before the return statement.

$matches[1]=round((float)str_replace(" ","",$matches[1]),0);

This function need to run 20++ times on my website. Because it's a table or values. So the processing speed will be abit slow compare to normal website. I have also added a ajax type loading image - Ajaxload.info as if it really loads from some where. Its a useful site where you can select the type and color you want for the animated image and it's Free. Just a trick to let people know why is the page loading is slow. Rather to let them wait for a blank page and they might switch if off.

No comments:

LinkWithin

Blog Widget by LinkWithin