REST API Tutorial

Currency Conversion Using Google currency converter Api

Summary of learning

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 […]

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.

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 =”PKR”; $converted_currency = currencyConverter($from_curr, $to_curr, $amount); ?>

    • Amount : 1.0
    • Amount Currency From : USD
    • Converted currency to : PKR

Converted USD to PKR

1 USD = PKR

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.

Currency Conversion Using Google currency converter Api

// 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 ="PKR";

 

 

 

 

$converted_currency = currencyConverter($from_curr, $to_curr, $amount);
?>

 

    • Amount : 1.0
    • Amount Currency From : USD
    • Converted currency to : PKR

Converted USD to PKR

1 USD = PKR

If you have requirements that needs our attentioN

Please feel free to contact us

We would love to hear from you

Share with your network

Facebook
Twitter
LinkedIn
WhatsApp
Email

This tutorial is intended to cover all the essentials steps and insights to understand Currency Conversion Using Google currency converter Api

Genislab Technologies

Learn further