Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bing search API and Azure

Tags:

php

search

bing

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 be replaced soon (1st Aug 2012)
  • The new API is known as Windows Azure Marketplace.
  • You use different URL for the two.

In the old API (Bing Search API 2.0), you specify a key (Application ID) in the URL, and such key will be used to authenticate the request. As long as you have the key as a parameter in the URL, you can obtain the results.

In the new API (Windows Azure Marketplace), you do NOT include the key (Account Key) in the URL. Instead, you put in a query URL, then the server will ask for your credentials. When using a browser, there will be a pop-up asking for a/c name and password. Instruction was to leave the account name blank and insert your key in the password field.

Okay, I have done all that and I can see a JSON-formatted results of my search on my browser page.

How do I do this programmatically in PHP? I tried searching for the documentation and sample code from Microsoft MSDN library, but I was either searching in the wrong place, or there are extremely limited resources in there.

Would anyone be able to tell me how do you do the "enter the key in the password field in the pop-up" part in PHP please?

Thanks alot in advance.

like image 520
Gapton Avatar asked Jun 01 '12 04:06

Gapton


People also ask

Is Bing in Azure?

We are Transitioning To reach out to wider audiences, Bing Search APIs will be transitioning from Azure Cognitive Services Platform to Azure Marketplace. Beginning October 31st, 2020, provisioning of any new instances of Bing Search APIs will need to be done via Azure Marketplace.

Does Bing have an API?

Use Bing APIs to enable search capabilities in your applications.

What is Bing Search API?

Bing Web Search API provides a list of related searches made by others, which can help end users refine their online search.

What does Bing API do?

The Bing Web Search API is a RESTful service that provides instant answers to user queries. Search results are easily configured to include web pages, images, videos, news, translations, and more.


Video Answer


1 Answers

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('<ul ID="resultList">');      foreach($jsonobj->d->results as $value)     {                                 echo('<li class="resultlistitem"><a href="'                  . $value->URL . '">'.$value->Title.'</a>');     }      echo("</ul>"); } 

Using cURL

If cURL is installed, which is normal these days:

<?php   $query = $_POST['searchText'];    $accountKey = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';   $serviceRootURL =  'https://api.datamarket.azure.com/Bing/Search/';     $webSearchURL = $serviceRootURL . 'Web?$format=json&Query=';    $request = $webSearchURL . "%27" . urlencode( "$query" ) . "%27";    $process = curl_init($request);   curl_setopt($process, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);   curl_setopt($process, CURLOPT_USERPWD,  "$accountKey:$accountKey");   curl_setopt($process, CURLOPT_TIMEOUT, 30);   curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);   $response = curl_exec($process);   $response = json_decode($response);    echo "<ol>";   foreach( $response->d->results as $result ) {     $url = $result->Url;     $title = $result->Title;      echo "<li><a href='$url'>$title</a></li>";   }   echo "</ol>"; ?> 

[WTS] changed SearchWeb to Search.

like image 138
John C Avatar answered Sep 21 '22 01:09

John C