Skip to main content
currency conversion using google api

Currency conversion in PHP Using Google API

Most of us use Google for searching web information. Google also provides Currency Converter API for converting currency to our desired currency. We can see the current status of our currency into other currency by going through Google’s currency converter page. However, sometimes we need to implement Google currency converter functionality on our application. This is very simple; you can implement it very easily with Google currency convert API using PHP.

Here in this tutorial, you will learn how to implement currency conversion functionality using PHP with demo.

You can view the live demo from the Demo link and can download the script from the Download link below.
Demo Download

Below is complete PHP currency converter script using Google API which have a function currencyConverter($from_Currency,$to_Currency,$amount) with three parameters. The first parameter is $from_Currency, the second is $to_Currency means the currency in which it is converted. And third parameter is $amount that will be passed for conversion.

PHP currency convert  functions:
<?php
function currencyConverter($from_Currency,$to_Currency,$amount) {
$from_Currency = urlencode($from_Currency);
$to_Currency = urlencode($to_Currency);
$encode_amount = 1;
$get = file_get_contents("https://www.google.com/finance/converter?a=$encode_amount&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;
}
?>

PHP currency convert  function call:
<?php
// change amount according to your needs
$amount =10;
// change From Currency according to your needs
$from_Curr =“INR”;
// change To Currency according to your needs
$to_Curr =“USD”;
$converted_currency=currencyConverter($from_Curr, $to_Curr, $amount);
// Print outout
echo $converted_currency;
?>

4 thoughts on “Currency conversion in PHP Using Google API

Leave a Reply

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

Shares

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!