How To Build a Currency Converter Using Bing Search (Bing.com)

Authors: CodeToday | PHP Code | Views: 68 | Posted: 09 AM: 10/09/2017

As the previous article How to Currency Converter Using Google API by PHP Code, I have guided you to build a currency converter from Google API, today I will guide you to use bing.com to search and convert currency using php code.

Separating data from bing.com may have a limit, you need to keep in mind the number of queries

The search for currency conversion from bing.com gives us the image below


currency converter using bing search

The syntax for searching on bing.com is: amount from(ex:usd) to(ex:eur)
URL from Bing.com:
 
https://www.bing.com/search?q=1+usd+to+eur

Full Source Code (HTML, PHP)

<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>How To Build a Currency Converter Using Bing Search (Bing.com)</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1>How To Build a Currency Converter Using Bing Search (Bing.com)</h1>
<form action="" method="post" class="form-horizontal">
<div class="form-group">
<input type="text" name="amount"  value="<?=$_POST['amount']?>" size="80" class="form-control" placeholder = "Enter amount here ..." required/>
</div>
<div class="form-group">
<select name = "from" class="form-control">
<option  value="USD">US Dollar ($)</option>
<option  value="UYU">Uruguayan Peso (UYU)</option>
<option  value="UZS">Uzbekistani Som (UZS)</option>
<option  value="VEF">Venezuelan Bolívar (VEF)</option>
<option  value="VND">Vietnamese Dong (₫)</option>
<option  value="VUV">Vanuatu Vatu (VUV)</option>
<option  value="WST">Samoan Tala (WST)</option>
</select>
</div>
<div class="form-group">
<select name = "to" class="form-control">
<option  value="USD">US Dollar ($)</option>
<option  value="UYU">Uruguayan Peso (UYU)</option>
<option  value="UZS">Uzbekistani Som (UZS)</option>
<option  value="VEF">Venezuelan Bolívar (VEF)</option>
<option  value="VND">Vietnamese Dong (₫)</option>
<option  value="VUV">Vanuatu Vatu (VUV)</option>
<option  value="WST">Samoan Tala (WST)</option>
</select>
</div>
<div class="form-group">
<button class="btn btn-info" type="submit" name="submit"><i class="glyphicon glyphicon-search"></i> Currency Converter</button>
</div>
</form>
<?
if(isset($_POST['submit'])) {

$amount = trim($_POST['amount']);
$from   = trim($_POST['from']);
$to     = trim($_POST['to']);

echo '<h3 class = "text-primary">Currency Converter '.$amount.' from '.$from.' to '.$to.'</h3>';

$url  = "http://www.bing.com/search?q=".$amount."+".$from."+".$to."";
$doc = new DomDocument;
$data = curlGet($url);
@$doc-> loadHTML(mb_convert_encoding($data, 'HTML-ENTITIES', 'UTF-8'));
$xpath = new DOMXPath($doc);
	foreach($xpath->query('//input[@id="cc_tv"]') as $value) {
		$rateHome = $value->getAttribute('value');
		echo '<p>'.$_POST['amount'].' '.$_POST['from'].' = <b>'.$rateHome.' '.$_POST['to'].'</b></p>';
	}
}
function curlGet($url){
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
	curl_setopt($ch, CURLOPT_HTTPGET, 1 );
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION , 1 );
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION , 1 );
	curl_setopt($ch, CURLOPT_URL, $url );
	curl_setopt($ch, CURLOPT_REFERER, $ref );
	curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);     
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
	$data = curl_exec($ch);
	curl_close($ch);
	return $data;
}
?>
</div>
</body>
</html> 
After you run the above code you will get the results as shown below:

How To Build a Currency Converter Using Bing Search (Bing.com)

So we have two ways to build a currency converter tool, using google API and bing.com search

If you have any questions, please leave a message below