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.
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("",$get);
$get = explode("",$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,
// 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); ?>
- Amount : 1.0
- Amount Currency From : USD
- Converted currency to : INR
Converted USD to INR
1 USD = INR