Bing search API and Azure

2019-01-08 15:27发布

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.

标签: php search bing
7条回答
家丑人穷心不美
2楼-- · 2019-01-08 16:09

http://www.guguncube.com/2771/python-using-the-bing-search-api

it contains python code to query the bing and this is according to latest new API (Windows Azure Marketplace)

# -*- coding: utf-8 -*-
import urllib
import urllib2
import json

def main():
    query = "sunshine"
    print bing_search(query, 'Web')
    print bing_search(query, 'Image')

def bing_search(query, search_type):
    #search_type: Web, Image, News, Video
    key= 'YOUR_API_KEY'
    query = urllib.quote(query)
    # create credential for authentication
    user_agent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; FDM; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 1.1.4322)'
    credentials = (':%s' % key).encode('base64')[:-1]
    auth = 'Basic %s' % credentials
    url = 'https://api.datamarket.azure.com/Data.ashx/Bing/Search/'+search_type+'?Query=%27'+query+'%27&$top=5&$format=json'
    request = urllib2.Request(url)
    request.add_header('Authorization', auth)
    request.add_header('User-Agent', user_agent)
    request_opener = urllib2.build_opener()
    response = request_opener.open(request) 
    response_data = response.read()
    json_result = json.loads(response_data)
    result_list = json_result['d']['results']
    print result_list
    return result_list

if __name__ == "__main__":
    main()
查看更多
登录 后发表回答