Monday 27 August 2012

Currency Converter


Hello friends,

welcome back to my blog. If you are making a e-commerce website and you want to display amount in many more than one currency. for that one way that you have a current rate of particular currency and you make the function that return you amount in different currency. But in this way you have to check manually currency rate and store in some table from that you can access it. But this way is time wasting and not user friendly. The accrual system should get latest currency rate automatically so that administrator of website has no worry about the latest currency rate. So i have mentioned how to get latest currency rate and use it, let's use below code.


<?php
function get_currency($from,$to)
{
 $url = 'http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s='. $from . $to .'=X';
 $handle = fopen($url, 'r');

 if($handle)
 {
     $result = fgets($handle, 4096);
     fclose($handle);
 }
 $allData = explode(',',$result); /* Get all the contents to an array */
 $curValue = $allData[1];
 return $curValue;
}
?>

you can see in this i have used yahoo's api, so it will get currency rate form yahoo and return you. But before using this be sure that  allow_url_fopen is on in your php.ini file. If it will off that it will return you warning. so first make allow_url_fopen on in your php.ini file.

As mentioned below you can call this function.

<?php
$dValue=get_currency("GBP","USD");
?>

Here the first parameter is from and second is to, as per this code this function will return 1 GBP= how many USD.

I hpe this will useful to you. If you have any query regarding this please tell me.

1 comment:

  1. Are you looking for php developer gurugram, india then you are at right place. Intileo Technologies are providing all IT services including php developement at reasonable rates. for any quary visit us :- https://intileo.com/ or call now :- 918470058143

    ReplyDelete