Bing search API and Azure

后端 未结 7 1015
滥情空心
滥情空心 2020-12-01 04:31

I am trying to programatically perform a search on Microsoft Bing search engine.

Here is my understanding:

  • There was a Bing Search API 2.0 , which will
7条回答
  •  孤独总比滥情好
    2020-12-01 05:14

    Documentation for new services can get a bit interesting - especially in the rabbit-warren of MSDN. The most clear explanation I can find is on the Migration Guide from this Bing Search API page. Best of all the migration guide has a nice simple example in PHP towards the end.

    EDIT: Alright, the migration guide is a starting point, but it isn't the best example. Here are two methods that work for me (no proxy, firewalls etc. interfering):

    Using file_get_contents

    Note: 'allow_url_fopen' needs to be enabled for this to work. You can use ini_set (or change php.ini etc.) if it isn't.

    if (isset($_POST['submit'])) 
    {
    
        // Replace this value with your account key
        $accountKey = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=';            
        $ServiceRootURL =  'https://api.datamarket.azure.com/Bing/Search/';                    
        $WebSearchURL = $ServiceRootURL . 'Web?$format=json&Query=';
    
        $cred = sprintf('Authorization: Basic %s', 
          base64_encode($accountKey . ":" . $accountKey) );
    
        $context = stream_context_create(array(
            'http' => array(
                'header'  => $cred
            )
        ));
    
        $request = $WebSearchURL . urlencode( '\'' . $_POST["searchText"] . '\'');
    
        $response = file_get_contents($request, 0, $context);
    
        $jsonobj = json_decode($response);
    
        echo('");
    }
    

    Using cURL

    If cURL is installed, which is normal these days:

    ";
      foreach( $response->d->results as $result ) {
        $url = $result->Url;
        $title = $result->Title;
    
        echo "
  • $title
  • "; } echo ""; ?>

    [WTS] changed SearchWeb to Search.

提交回复
热议问题