How to Convert Currency using Google Finance in PHP
In this tutorial I am going to show you How to 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.
Index.php Contains PHP code to show drop-down lists for conversion with a text box.
<?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.
I hope this simple tutorial help you and please don’t forget to give us your feedback.
Tutorial Categories:
Cool! Well written. Do you know, if Google allows us officially to use it that way?
Google is super smart, I don’t think so that its against there rules that’s why I am able to use it..
How do I make it work in a html page? Thanks
Hi, i`ve added my e-mail address to the subscribers list and i still can`t download the code 🙁