top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to Convert Currency using Google Finance in PHP

+2 votes
356 views

Sample code may be helpful?

posted Mar 17, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Use Google finance Currency Converter and convert currencies very easily in PHP. It takes an amount and 2 currencies from and to and then send it to Google finance using HTTP request and get the amount value converted in given currency, we have used curl to send request. In final stage we parse response and show results.

<?php
include "convert.php";
$from       = isset($_POST['from']) ? $_POST['from'] : '';
$to         = isset($_POST['to']) ? $_POST['to'] : '';
$amount     = isset($_POST['amount']) ? $_POST['amount'] : '';
$content = "";
if($_POST){
    if(!is_numeric($amount)){
    $content .= "<br><span style='background-color:red;padding:5px;color:#fff;'>Invalid amount.</span>";
    }
    elseif($from == $to){
    $content .= "<br><span style='background-color:red;padding:5px;color:#fff;'>Please select distinct currencies.</span>";
    }
    else{
    $rawData = currencyConvert($from,$to,$amount);
    $regex  = '#\<span class=bld\>(.+?)\<\/span\>#s';
    preg_match($regex, $rawData, $converted);
    $result = $converted[0];
    if($result == ""){
        $content .= "<br><span style='background-color:red;padding:5px;color:#fff;'>Exchange Rate not available.</span>";
    }
    else{
        $content .= "<br><span style='background-color:lime;padding:5px;'>".$amount." ".$from." = ".$result."</span>";
    }
    }    
}
$listFrom = '
<select name="to">
<option  value="AED">United Arab Emirates Dirham (AED)</option>
<option  value="EUR">Euro (€)</option>
<option  value="GBP">British Pound Sterling (£)</option>
<option  value="INR">Indian Rupee (Rs.)</option>
<option  value="PKR">Pakistani Rupee (PKR)</option>
<option  value="SGD">Singapore Dollar (SGD)</option>
<option  value="USD">US Dollar ($)</option>
</select>
';

$listTo = '
<select name="to">
<option  value="AED">United Arab Emirates Dirham (AED)</option>
<option  value="EUR">Euro (€)</option>
<option  value="GBP">British Pound Sterling (£)</option>
<option  value="INR">Indian Rupee (Rs.)</option>
<option  value="PKR">Pakistani Rupee (PKR)</option>
<option  value="SGD">Singapore Dollar (SGD)</option>
<option  value="USD">US Dollar ($)</option>
</select>
';
$listFrom  = str_replace("\"$from\"","\"$from\" selected",$listFrom); // Make dropdown selected
$listTo    = str_replace("\"$to\"","\"$to\" selected",$listTo); // Make dropdown selected

$content .='<form action="" method="post" name="f">
<input name="amount" maxlength="12" size="5" autocomplete="off" value="'.$amount.'"><br />
<div>
'.$listFrom.'
</div>
<div style="padding: 6px 8px">to</div>
<div>
'.$listTo.'
</div>
<input type=submit value="Convert">
</form>';
?>

In drop-down we have added few values you can get complete drop-down by downloading script. Included convert.php file contains a function which send HTTP request and return response back. convert.php Contains a function which send HTTP request and return response back.

<?php
function currencyConvert($from,$to,$amount){

    $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); 

    return $response;
} 
?>

This function accept 3 parameters and send request to Google finance using HTTP and it will return complete code.

answer Mar 21, 2016 by Manikandan J
Similar Questions
0 votes

I want to retrieve the user's location after user signs in my website. How can I do that?

At this moment here is the scope that i have: $client->setScopes('email');
I guess I have to change that.

And to retrieve data I have the following:
$client->verifyIdToken()->getAttributes();

Do I need another method than this?

+1 vote

I have a main domain (of course) and a sub domain. I'm really trying to steer my personal stuff away from the main one and have focused all of my php development to the sub-domain.

Lately I noticed that google catalogs my sub-domain site stuff under the main domain name and the links that come up lead to that domain name with the path that takes the user to the sub-domain's home folder and beyond.

Is there something that php (apache??) can do to control either google's robots or the user's view (url) so that it appears as a page of my sub-domain?

...