Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Bing Test</title>
- </head>
- <body>
- <?php
- /****
- * Simple PHP application for using the Bing Search API
- */
- $acctKey = 'Aj/L2hjw3F+YoU+cj4uxIO6cHbAdzRYSifcjBALMD+o';
- $rootUri = 'https://api.datamarket.azure.com/Bing/Search';
- // Read the contents of the .html file into a string.
- $contents = file_get_contents('Bing_Test.html');
- if ($_POST['query'])
- {
- // Here is where you'll process the query.
- // Encode the query and the single quotes that must surround it.
- $query = urlencode("'{$_POST['query']}'");
- // Get the selected service operation (Web or Image).
- $serviceOp = $_POST['service_op'];
- // Construct the full URI for the query.
- $requestUri = "$rootUri/$serviceOp?\$format=json&Query=$query";
- // The rest of the code samples in this tutorial are inside this conditional block.
- // Encode the credentials and create the stream context.
- $auth = base64_encode("$acctKey:$acctKey");
- $data = array(
- 'http' => array(
- 'request_fulluri' => true,
- // ignore_errors can help debug – remove for production. This option added in PHP 5.2.10
- 'ignore_errors' => true,
- 'header' => "Authorization: Basic $auth")
- );
- $context = stream_context_create($data);
- // Get the response from Bing.
- $response = file_get_contents($requestUri, 0, $context);
- // Decode the response.
- $jsonObj = json_decode($response);
- $resultStr = '';
- // Parse each result according to its metadata type.
- 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;
- }
- }
- // Substitute the results placeholder. Ready to go.
- $contents = str_replace('{RESULTS}', $resultStr, $contents);
- }
- echo $contents;
- ?>
- ????
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement