I wants to fetch the bing api results but not succeeded on that. Already used many codes and samples but didnt get my answer. Please is dere any mistake in my code or not.
There are two files 1. bing.php (HTML) 2. bing_code.php (PHP)
<html>
<head>
<title>Bing Search Tester (Basic)</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<h1>Bing Search Tester (Basic)</h1>
<form method="POST" action="bing_code.php">
<label for="service_op">Service Operation</label><br/>
<input name="service_op" type="radio" value="Web" CHECKED /> Web
<input name="service_op" type="radio" value="Image" /> Image <br/>
<label for="query">Query</label><br/>
<input name="query" type="text" size="60" maxlength="60" value="" /><br /><br />
<input name="bt_search" type="submit" value="Search" />
</form> <h2>Results</h2>
</body>
</html>
Above is the html code below is php code
<?php
$acctKey = 'key';
$rootUri = 'https://api.datamarket.azure.com/Bing/Search';
$contents = file_get_contents('bing.php');
if ($_POST['query'])
{
$query = urlencode("'{$_POST['query']}'");
$serviceOp = $_POST['service_op'];
$requestUri = "$rootUri/$serviceOp?\$format=json&Query=$query";
$auth = base64_encode("$acctKey:$acctKey");
$data = array('http' => array('request_fulluri' => true,'ignore_errors' => true,'header' => "Authorization: Basic $auth"));
$context = stream_context_create($data);
$response = file_get_contents($requestUri, 0, $context);
$jsonObj = json_decode($response, true);
print_r($jsonObj); echo "nothing!"; die();
$resultStr = '';
if( ( is_array( $jsonObj->d->results )) && ( ! empty( $jsonObj->d->results ) ) ) {
foreach($jsonObj->d->results as $value)
{
switch ($value->__metadata->type)
{
case 'WebResult':
$resultStr .= "<a href=\"$value->Url\">{$value->Title}</a><p>{$value->Description}</p>";
break;
case 'ImageResult': $resultStr .= "<h4>{$value->Title} ({$value->Width}x{$value->Height}) " . "{$value->FileSize} bytes)</h4>" . "<a href=\"{$value->MediaUrl}\">" . "<img src=\"{$value->Thumbnail->MediaUrl}\"></a><br />";
break;
}
}
} else {
if( ! is_array( $jsonObj->d->results )) {
echo "jsonObj->d->results is not an array!";
} elseif( empty( $jsonObj->d->results )) {
echo "jsonObj->d->results is empty!";
}
}
$contents = str_replace('{RESULTS}', $resultStr, $contents);
}
echo $contents;
?>