How to convert currency using Google API and PHP

Today we are going to create a Currency Converter using API.There are many currency convert API but today we are using Google Finance Currency Converter.

It’s very simple to create a currency converter using Google Finance Currency Converter because its need only 3 parameters : From Currency,To Currency and amount.Using these three values we will send HTTP REQUEST to Google Finance Currency Converter (http://www.google.com/finance/converter) using curl and we will get a response in HTML Format.After getting the Response we will parse the HTML data through regular expression and get the converted amount.

<?php
function convertCurrency($from="USD",$to="INR",$amount=1){
    $url = "http://www.google.com/finance/converter?a=$amount&from=$from&to=$to"; 
    $request = curl_init(); 
    $timeOut = 0; 
    curl_setopt ($request, CURLOPT_URL, $url); 
    curl_setopt ($request, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt ($request, CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"); 
    curl_setopt ($request, CURLOPT_CONNECTTIMEOUT, $timeOut); 
    $response = curl_exec($request); 
    curl_close($request); 
    $regularExpression     = '#\<span class=bld\>(.+?)\<\/span\>#s';
    preg_match($regularExpression, $response, $finalData);
    return $finalData[0];
 } 

?>

Now Let me explain the Above code.We have created a Function name convertCurrency() which requires(/optional) three Parameters:
1.From Currency ($from)
2.To Currency ($to)
3.Amount ($amount)

We had set the Default values to each parameters that means if you will not pass the values to parameters it will use the default values i.e From Currency = USD , To Currency = INR, Amount =1 .

Now if you wants to convert any Currency Just use the Above function e.g:

<?php
$convertedAmount = convertCurrency("BTC","INR",1);
echo "1 BTC = ".$convertedAmount;
?>

and the Output will be 1 BTC = 17631.6688 INR.

Full Currency Converter PHP Code

<?php
function convertCurrency($from="USD",$to="INR",$amount=1){
    $url = "http://www.google.com/finance/converter?a=$amount&from=$from&to=$to"; 
    $request = curl_init(); 
    $timeOut = 0; 
    curl_setopt ($request, CURLOPT_URL, $url); 
    curl_setopt ($request, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt ($request, CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"); 
    curl_setopt ($request, CURLOPT_CONNECTTIMEOUT, $timeOut); 
    $response = curl_exec($request); 
    curl_close($request); 
    $regularExpression     = '#\<span class=bld\>(.+?)\<\/span\>#s';
    preg_match($regularExpression, $response, $finalData);
    return $finalData[0];
 } 


$convertedAmount = convertCurrency("BTC","INR",1);
echo "1 BTC = ".$convertedAmount;
?>

Shubham Kumar

Hey, I am Shubham and i love Blogging, Coding and exploring new things and obviously sharing my experience with you.

You may also like...

Leave a Reply

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