Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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_Array.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.
- //EXAMPLE: https://api.datamarket.azure.com/Bing/Search/Web?$format=json&Query=%27Xbox%27&$skip=50
- $requestUri = "$rootUri/$serviceOp?\$format=json&Query=$query";
- $requestUri = "$rootUri/$serviceOp?\$format=json&Query=$query&\$skip=51";
- // 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.
- $js = json_decode($response);
- // Declaring Variables for the values from the results and displayed in string
- $link = '';
- $title = '';
- $snippet = '';
- $resultStr = '';
- // Array created to store the query results
- $results = array ();
- //The variables will now be populated with values from the json(results)
- foreach($js->d->results as $item){
- $link = $item->Url;
- $title = $item->Title;
- $snippet = $item->Description;
- $results[] = array($link, $title, $snippet);
- }
- //Display results back that are stored in the array
- foreach($results as $key=>$value){
- $resultStr .= '<a href="'. $value[0] . '">' . $value[1] .'</a>'
- . "<br />link: " . $value[0]
- . "<br />snippet: " . $value[2]
- . "<br /><br />" ;
- }
- // Substitute the results placeholder. Ready to go.
- $contents = str_replace('{RESULTS}', $resultStr, $contents);
- }
- echo $contents;
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement