Currency convertion using google api in php

We all are using google. Any thing we are finding in web we first trust on google and we believe that google output is ten times better than others. Whether we talk about searching, charting, Apis etc.
So here we are talking about api for currency conversion. There are lots of conversion apis you can find but there drawback is all they are paid and they have some limitation but when you are using google api its absolutely free without any limitation.

Now i am going to create a function "currencyExchange" to convert any amount.

//This Function Is Use To Get The Current Currency Exchange Rate.
function currencyExchange($from_currency, $to_currency='INR', $amount = 1) {
 $from_currency = urlencode($from_currency);
 $to_currency = urlencode($to_currency); 
 $url = "https://www.google.com/finance/converter?a=$amount&from=$from_currency&to=$to_currency";
 $getUrl = file_get_contents($url);
 $objUrl = explode("<span class=bld>",$getUrl);
 $objUrl = explode("</span>",$objUrl[1]);
 $result_currency = preg_replace("/[^0-9\.]/", null, $objUrl[0]);
 return $result_currency;
}


Method of calling function :  

Suppose you want to convert your 10 Rupee(INR) into USD. So below is the parameter you have to pass in "currencyExchange" function -

//change amount according to your needs
$amount =10;

//change From Currency according to your needs
$from_Curr = "INR";

//change To Currency according to your needs
$to_Curr = "USD";
$result_currency = currencyExchange($from_Curr, $to_Curr, $amount);

//Print outout
echo $result_currency;
If you like this post so please leave a comment.

Chears
Happy Coding :) 


Post a Comment

Previous Post Next Post