How to Currency Converter using PHP and Google API

Google is providing free of cost Currency Converter API to convert one currency into another currency type. Google Currency Converter API can implement any programming language. I am using PHP to create a currency converter application to convert real-time currency value from one currency type to another currency type.

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;
}
?>

Leave a Reply

Your email address will not be published. Required fields are marked *