none
How to show the firt five URL result?

    Question

  • Hi,i'm using the API search,but the Api returns a lot of URL,i've need the first five result.

    i tryed with $top=5 in the query but no work.

    Can someone help me?

    <?php 
    
    function search($searchstr) {
    
    
    /****
    
    * Simple PHP application for using the Bing Search API
    
    */
    
    $acctKey = 'personal ID';
    
    $rootUri = 'https://api.datamarket.azure.com/Bing/Search';
    
    // Read the contents of the .html file into a string.
    
    $contents = 'vuoto';
    
    
    
    
    {
    
    // Here is where you'll process the query.
    
    // Encode the query and the single quotes that must surround it.
    
    $query = urlencode($searchstr);
    
    // Get the selected service operation (Web or Image).
    
    $serviceOp = 'Web';
    // 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); 
    echo $response;
    
    // 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; 
     
    	}
    } 
    // Substitute the results placeholder. Ready to go. 
    
    $contents = str_replace('vuoto', $resultStr, $contents); 
    
    }

    This is my code,pls help me.

    Wednesday, August 07, 2013 11:32 AM

Answers

  • Hi,

      >> i tryed with $top=5 in the query but no work.

    From my experience, $top would work fine. But based on your provided code, I don't see you use it in your URI. In addtion, it seems you have an extra back slash in your URI. Please remove it to see whether it works.
     
    $rootUri/$serviceOp? \ $format=json&Query='$query'

    Best Regards,

    Ming Xu


    Ming Xu
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, August 08, 2013 2:28 PM