Currency Converter In PHP Using Google Api

Google is provides free of cost Currency Converter API to convert one currency into another currency type.Google Currency Converter API can be implement any programming language. I am suing PHP to create currency converter application to convert real time currency value from one currency type to other currency type.In this tutorials, I will learn how to implement currency conversion functionality using PHP with demo.

I will provide two script to convert currency using google API one is by using file_get_contents() method and other is using PHP curl.We will create a PHP function and pass three parameters like From currency, To currency and the amount to be converted, below php script are used to convert currency from any currency to another currency using Google Currency Converter API.

It’s Deprecated

Google Currency Converter Api Using file_get_contents() PHP method

<?php
function currencyConverter($from_currency, $to_currency, $amount) {
$from_Currency = urlencode($from_currency);
$to_Currency = urlencode($to_currency);
$encode_amount = urlencode($amount);
$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;
}
?> 

Google Currency Converter Api Using PHP Curl

<?php
function currencyConverter($from_currency, $to_currency, $amount) {
$amount    = urlencode($amount);
$from    = urlencode($from_currency);
$to        = urlencode($to_currency);
$url    = "http://www.google.com/ig/calculator?hl=en&q=$amount$from=?$to";
$ch     = @curl_init();
$timeout= 0;
 
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch,  CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
 
$rawdata = curl_exec($ch);
curl_close($ch);
$data = explode('"', $rawdata);
$data = explode(' ', $data['3']);
$var = $data['0'];
return round($var,3);
}
?>

We have created a function currencyConverter($from_currency,$to_currency, $amount) with three parameters, where the parameters are,

  • $from_currency: This parameters will use to which currency you want to convert.
  • $to_currency: This parameters will use to currency, in which it will convert.
  • $amount: How much amount will convert.

How to call Above Currency Converter Method

<?php
//amount
$amount =10;
 
//From Currency
$from_currency ="INR";
 
//To Currency
$to_currency ="USD";
 
$converted_currency = currencyConverter($from_currency, $to_currency, $amount);
 
echo $converted_currency;
?>

You can download source code and Demo from below link.

5 thoughts on “Currency Converter In PHP Using Google Api

  1. Does your blog have a contact page? I’m having trouble locating it but, I’d like to shoot you an email.
    I’ve got some recommendations for your blog you might be interested in hearing. Either way, great blog and I look forward to seeing it grow over time.

Leave a Reply

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