How to Currency Converter using PHP and Google API

0

Google is provides free of cost Currency Converter API to convert one currency into another currency type.Google Currency Converter API can be implement any programming language. I am suing PHP to create currency converter application to convert real time currency value from one currency type to other currency type. You may also like How to convert Seconds into Minutes and Hours using PHP and How to convert bytes Into KB, MB and GB using PHP.

HTML Code

<html>
    <head>
    </head>
    <body>
        <form method="post"action="">
                <input type="text" name="amount" placeholder="Enter Amount">
                <select name="convert_from">
                    <option  value="INR">Indian Rupee</option>
                    <option  value="USD">US Dollar</option>
                    <option  value="SGD">Singapore Dollar</option>
                    <option  value="EUR">Euro</option>
                    <option  value="AED">UAE Dirham</option>
                </select>
                <select name="convert_to">
                    <option  value="INR">Indian Rupee</option>
                    <option  value="USD">US Dollar</option>
                    <option  value="SGD">Singapore Dollar</option>
                    <option  value="EUR">Euro</option>
                    <option  value="AED">UAE Dirham</option>
                </select>
                <br>
                <input type="submit" name="convert_currency" value="Convert Currency">
        </form>
    </body>
</html>

PHP Code

<?php
function currency_converter($from,$to,$amount){
	$data = file_get_contents("https://www.google.com/finance/converter?a=$amount&from=$from&to=$to");
	preg_match("/<span class=bld>(.*)<\/span>/",$data, $converted);
	$converted = preg_replace("/[^0-9.]/", "", $converted[1]);
	return number_format(round($converted, 3),2);
}


if(isset($_POST['convert_currency']))
{
    $amount=$_POST['amount'];
    $from=$_POST['convert_from'];
    $to=$_POST['convert_to'];
    $rawData = currency_converter($from,$to,$amount);
    echo $rawData;
}
?>