📜 ⬆️ ⬇️

Bing API + PHP

Finding 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 an API key at www.bing.com/developers/appids.aspx

Step 2:
We determine the main search parameters, namely:

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

Step 3:
We form the query string and process the received data. For example:
$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 given 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 executed, 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 what is important to know:
Search Bing API can search only on a specific site. For example, to search only on the site habrahabr.ru , the above search query must 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 handle Cyrillic queries. For example, if you include in the search query filtering obscene content, as it turns out that the search engine hardly filters the content, if the query 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, the 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 morphological search and the speed of indexing site content. Personally, I had to pay attention to Bing, upgrading the search on one of the sites with technical documentation
PS: The simplest example of Bing API + PHP for site search: docs.google.com/View?id=dmwh7bn_10fhhxgtfs

Source: https://habr.com/ru/post/86172/


All Articles