This php tutorial help to create simple application to convert currency from one to another desired currency using google finance currency converter api. Google providing many useful api and libs for developers.Google finance api is another popular api for real time currency conversion from one currency to another currency.You can easily check real time currency rate using google api.
google finance currency converter is api so its can be integrate easily with any programming languages.I am using PHP to create simple page for currency converter using google api.
Table of Contents
Currency Conversion Using Google Currency API
We will learn how to implement currency conversion functionality using PHP with api. I will share php script that will call google converter api and display result. We will pass three parameters in php method that send to google api to get real time currency converted result.The parameters are:
$amount :
The amount money which need to converted.
$from_currency :
The amount in which currency are.
$to_currency :
The currency in which it will converted.
We will create currencyConverter($amount, $from_currency, $to_currency)
method in index.php
file.
1 2 3 4 5 6 7 8 9 |
function currencyConverter($amount, $from_currency, $to_currency) { $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; } |
We have created currency converter method and passed required parameters.We will call this method into index.php
file and display converted currency, We will add below code,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<div> // change amount as per your requirements $amount =1; // change From Currency as per your needs $from_curr ="USD"; // change To Currency according to your needs $to_curr ="INR"; $converted_currency = currencyConverter($from_curr, $to_curr, $amount); ?> <ul> <li><strong>Amount :</strong> 1.0</li> <li><strong>Amount Currency From :</strong> USD</li> <li><strong>Converted currency to :</strong> INR</li> </ul> <h3>Converted USD to INR</h3> <div class="alert alert-success">1 USD = <?php echo $converted_currency;?> INR</div> </div> </div> |
The Final PHP script for Google Currency Converter API
I am using bootstrap css framework for UI enhancement into this currency application, You can remove or change as per your requirement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> <title>Currency Coneversion Using Google currency converter Api</title> </head> <body> <div class="container"> <div class="col-sm-6" style="padding-top:50px;"> <div class="well"> <h2>Currency Conversion Using Google currency converter Api</h2> <div> // change amount as per your requirements $amount =1; // change From Currency as per your needs $from_curr ="USD"; // change To Currency according to your needs $to_curr ="INR"; $converted_currency = currencyConverter($from_curr, $to_curr, $amount); ?> <ul> <li><strong>Amount :</strong> 1.0</li> <li><strong>Amount Currency From :</strong> USD</li> <li><strong>Converted currency to :</strong> INR</li> </ul> <h3>Converted USD to INR</h3> <div class="alert alert-success">1 USD = <?php echo $converted_currency;?> INR</div> </div> </div> </div> </div> </div> </body> </html> <?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; } ?> |