Currency conversion in PHP Using Google API

0
280

Mostely of us use Google for searching web information. Google also provides Currency Converter API for converting currency to our exchange currency. We can see the current status of our currency into other currency by going through Google’s currency converter page. However, sometimes we need to implement Google currency converter functionality on our application. This is very simple; you can implement it very easily with Google currency convert API using PHP.

Here in this tutorial, you will study how to implement currency conversion functionality using PHP with demo.

Below is complete PHP currency converter script using Google API which have a function currencyConverter($from_Currency,$to_Currency,$amount) with three parameters. The first parameter is $from_Currency, the second is $to_Currency means the currency in which it is converted. And third parameter is $amount that will be passed for conversion.

PHP currency exchange functions:
<?php
function currencyConverter($from_Currency,$to_Currency,$amount) {
$from_Currency = urlencode($from_Currency);
$to_Currency = urlencode($to_Currency);
$get = file_get_contents(“https://finance.google.com/finance/converter?a=1&from=$from_Currency&to=$to_Currency”);
$get = explode(“<span class=bld>”,$get);
$get = explode(“</span>”,$get[1]);
$converted_currency = preg_replace(“/[^0-9\.]/”, null, $get[0]);
return $converted_currency;
}
?>

PHP currency exchange function call:
<?php
// 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”;
$converted_currency=currencyConverter($from_Curr, $to_Curr, $amount);
// Print outout
echo $converted_currency;
?>

You can view the live demo from the Demo link and can download the script from the Download link below.
Demo

 

Three Function   file  ANOTHER WAY TO CONVERT CURRENCY

use below function file that convert check one by one

function Google_Currency($amount, $from, $to) {
$matches = array();
if (preg_match(‘~<b>.+? = (.+?)</b></h2>~’, file_get_contents(‘http://www.google.com/search?q=’ . urlencode(floatval($amount) . ‘ ‘ . strtoupper($from) . ‘ to ‘ . strtoupper($to))), $matches) > 0) {
if (array_key_exists(1, $matches) === true) {
return floatval($matches[1]);
}
}
return false;
}

function convert_currency($from_Currency,$to_Currency,$amount) {
$amount = urlencode($amount);
$from_Currency = urlencode($from_Currency);
$to_Currency = urlencode($to_Currency);
$url = “http://www.google.com/ig/calculator?hl=en&q=$amount$from_Currency=?$to_Currency”;
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT , “Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)”);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$rawdata = curl_exec($ch);
curl_close($ch);
$data = explode(‘”‘, $rawdata);
$data = explode(‘ ‘, $data[‘3’]);
$var = $data[‘0’];
return round($var,3);
}

function Google_Search_Currency_Converter($amount,$from,$to,$showcurrency=”){
$content = file_get_contents(“http://www.xe.com/ucc/convert.cgi?From=” .$from. “&To=” .$to. “&Amount=” .$amount. “&template=pca-xetrade”);

//Define the HTML code we have to break down with a uniqe peice of HTML code found in that page.
$content = explode(‘<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0>’,$content);

//We will get rid of this line to get rid of the left over code above the bits we want to get to.
$content = str_replace(‘<TD VALIGN=MIDDLE ALIGN=RIGHT><FONT FACE=”Arial,Helvetica”><B>’, “”, “$content[1]”);

//Once again defining our way to the final converted number…
$content = explode(‘<TD COLSPAN=3 ALIGN=CENTER><FONT FACE=”Arial,Helvetica” SIZE=-2>’,$content);

//Remove every bit of HTML code left that isn’t numeric!
$toremove = array(“a”, “b”, “c”, “d”, “e”, “f”, “g”, “h”, “i”, “j”, “k”, “l”, “m”, “n”, “o”, “p”, “q”, “r”, “s”, “t”, “u”, “v”, “w”, “x”, “y”, “z”, “A”, “B”, “C”, “D”, “E”, “F”, “G”, “H”, “I”, “J”, “K”, “L”, “M”, “N”, “O”, “P”, “Q”, “R”, “S”, “T”, “U”, “V”, “W”, “X”, “Y”, “Z”, “>”, “<“, “\””, “\'”, “=”, “,”, “/”, “-1”);
$content = str_replace($toremove, “”, “$content[0]”);

//Final thing to remove, Woo Hoo! We will use ‘+1’ to sperate the two numeric values of the two currencys.
$final = explode(‘+1’,$content);

//Make the currency codes in upper case…
$from_code = strtoupper($from);
$to_code = strtoupper($to);

//The final conversion
//echo ‘<b>’ .$final[0]. ‘ ‘ .$from_code. ‘ =’ .$final[1]. ‘ ‘ .$to_code. ‘</b>’;

if($showcurrency!=”){
return round($final[1],2);
}
else{
return $to_code.’ ‘.round($final[1],2);
}
}

change.php page

<?php session_start();
$arr=array(“USD”,”EGP”,”KWD”,”SAR”,”AED”,”GBP”,”EUR”);
$curr=$_REQUEST[‘curr’];
if(in_array($curr,$arr))
{
$_SESSION[‘curr’]=$curr;
header(“Location:”.$_SERVER[‘HTTP_REFERER’]);
exit;
}
else
{
header(“location:index.php”);
exit;
}
?>

Drop down

<select class=”fs11 bdrn” name=”curr” style=”width:90px; border:1px solid #CCC; ” onchange=”window.location.href='<?php echo site_url(); ?>/change.php?curr=’+this.value;”>
<option value=”USD” <?php echo ($_SESSION[‘curr’]==”USD”)?”selected”:””;?>>USD</option>
<option value=”EGP” <?php echo ($_SESSION[‘curr’]==”EGP”)?”selected”:””;?>>Egyptian Pound</option>
<option value=”KWD” <?php echo ($_SESSION[‘curr’]==”KWD”)?”selected”:””;?>>Kuwaiti Dinar</option>
<option value=”SAR” <?php echo ($_SESSION[‘curr’]==”SAR”)?”selected”:””;?>>Saudi Riyal</option>
<option value=”AED” <?php echo ($_SESSION[‘curr’]==”AED”)?”selected”:””;?>>Emirates Diram</option>
<option value=”GBP” <?php echo ($_SESSION[‘curr’]==”GBP”)?”selected”:””;?>>British Pound</option>
<option value=”EUR” <?php echo ($_SESSION[‘curr’]==”EUR”)?”selected”:””;?>>Euro</option>
</select>