none
How to show Bing Results in separately? RRS feed

  • Question

  • I want to show Bing search results in separately. Meaning that each div will have one search result.

    I tried the Bing Basic but it shows all search results together in one div. I used this code:

    This is the PHP File:

    <?php
    
    /****
    
    * Simple PHP application for using the Bing Search API
    
    */
    
    $acctKey = '/tTjzCQHVQfJyCJyDYKrXMGwsjw5CZkRLyA8QxxY0cM=';
    
    $rootUri = 'https://api.datamarket.azure.com/Bing/Search';
    
    // Read the contents of the .html file into a string.
    
    $contents = file_get_contents('index.html');
    
    if ($_POST['query'])
    
    {
    
    // Here is where you'll process the query.
    
    // The rest of the code samples in this tutorial are inside this conditional block.
    
    // 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"; 
    
    // 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;
    
    ?> 

    This is that Index File:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head> <title>Bing</title> 
    <meta http-equiv="Content-Type" 
    content="text/html; charset=utf-8" />
    <link href="css.css" rel="stylesheet" type="text/css" />
    </head> 
    <body>
    <h1>Bing</h1>
    <form method="POST" action="bing_basic.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</h1>
    <div id="content">{RESULTS}</div>
    </body>
    </html> 


    • Edited by ImranSheikh Wednesday, April 24, 2013 5:23 AM
    Wednesday, April 24, 2013 5:22 AM