Bing API + PHP

    Searching from the Bing API is quick and easy! Today I would like to share with you some of the nuances of developing a web search.

    Step 1:
    Register and get the API key on the page www.bing.com/developers/appids.aspx

    Step 2:
    We determine the basic search parameters, namely:
    • Search query (URL encoding of a search query in accordance with RFC 1738, in UTF-8 text encoding)
    • Source Type (web, images, video, etc ...)
    • Number of results per query (maximum 50)
    • Offset to the desired position of the search results from the beginning (no more than 1000)
    • The format of the data expected from the search server (xml, json, soap)

    Additional options depend on the type of search source. Read more about this in the documentation.

    Step 3:
    Form a query string and process the received data. For instance:
    $url = 'http://api.bing.net/xml.aspx?Appid=' .$Appid. '&query=' .rawurlencode(iconv("CP1251", "UTF-8", searchtext)). '&sources=web&web.count=10&web.offset=0&web.filetype=html';

    $searchpage = file_get_contents($url);

    The results obtained in this case will be returned by the server in the form of XML containing prefixes in the namespace:
    $sxe = new SimpleXMLElement($data);
    $resultsearch = $sxe->children('web', TRUE);

    If the search query was successfully completed, the results will be contained in the $ resultsearch-> Web-> Results-> WebResult array, otherwise the error message will be contained in the $ sxe-> Errors-> Error array

    What is not in the documentation, but about what It is important to know:
    Search Bing API can only search a specific site. For example, to search only on the site habrahabr.ru , the above search query needs to be slightly modified:
    $url = 'http://api.bing.net/xml.aspx?Appid=' .$Appid. '&query=' .rawurlencode(iconv("CP1251", "UTF-8", searchtext)). '+site:habrahabr.ru&sources=web&web.count=10&web.offset=0&web.filetype=html';

    I would also like to say that the search does not always correctly process Cyrillic queries. For example, if you include filtering of obscene content in a search query, it turns out that the search engine practically does not filter the content if the request is in Russian:
      $url = 'http://api.bing.net/xml.aspx?Appid='.$Appid.'&query='.rawurlencode(iconv("CP1251","UTF-8",'секс')).'&sources=web&web.count=50&web.offset=0&Adult=Strict';

    * This source code was highlighted with Source Code Highlighter.

    Conclusion:
    Of course, a search in Cyrillic from Bing does not yet allow you to fully enjoy all the chips and goodies described in the documentation. But I would like to say that only practical application will allow you to compare the quality of the morphological search and the speed of indexing the content of the site. Personally, I had to pay attention to Bing, modernizing the search on one of the sites with technical documentation
    PS: The simplest example of the Bing API + PHP for searching the site: docs.google.com/View?id=dmwh7bn_10fhhxgtfs

    Also popular now: