Breaking News

Search This Blog

Saturday 7 January 2012


MSN search API with PHP


by David Callan

Everyone knows that the ability to search through all the content on a site is a big plus in terms of usability for that site, this is particularly true for very large sites which have massive amounts of archival and regularly updated content published. Manually hyperlinking from page to page on a website looking for what you want can be a pain, so the advantages of having an internal site search engine are obvious.
Webmasters who want search functionality for their website have a number of options in regards to getting that functionality, the most popular is by using some sort of search provider service/product such as 'Google Free' located at http://www.google.com/searchcode.html, http://www.picosearch.com/, http://www.freefind or http://www.atomz.com. Many of the aforementioned sites provide search capability free but some also offer more in depth commercial products for the more advanced users. The problem with using someone's else's search product is that your often limited in terms of the amount of customization you can do.
Another main way of enabling search on your site is of course to write your own search engine. That means indexes, caches, algorithms and all that stuff, If your into that kind of thing that's fine, personally though I would rather steer clear of programming my own search engine. It's just too much unnecessary work.
The final option which is becoming popular with many professional webmasters is to use the technology from one of the leading search engines by 'plugging' into their databases via their APIs (Application Program Interfaces) from any of the supported scripting languages. All the APIs work like standard issue functions and simply return raw data without presentation or style markup thus allowing you to customize your search page and results page interfaces to fit into your existing designs and styles. This tutorial will show you how to set up a PHP based internal search engine for your site by using the MSN search API. The relevant URL for this API is http://msdn.microsoft.com/live/msnsearch/default.aspx
Before we proceed a little note on why I chose the MSN search API as opposed to the Google API. Firstly Google will only allow 1,000 queries to their API per key while MSN will allow 10,000 queries per application, but more importantly Google will only return a maximum of 10 results per query while MSN on the other hand will return up to 50. This means that if for example you wanted 25 Google results on a page you would have to do multiple calls and use some complex array based code to store results 1-10 while your script has to call the Google API again to get results 11-20 and then again to get results 21-25. To get 25 results with MSN a single call is made therefore less complex code is needed to handle it, so for the purposes of keeping this tutorial as simple as possible I have chosen to use the MSN search API.

Search box interface code


Ok then lets jump into the code for the form and actually displaying the search box field (trivial I know). Mostly just HTML but since both the search box and results page will be generated using the same .php script we can (after submitting the search) display the searched for value in the search box with the results below it, a small bit of PHP code is required for this and is in blue below:


Site Search






Keyphrase:">
 

 

After the search form is submitted - making the call to the MSN search API

 
After pressing the search button the form is submitted to itself (msnsearch.php) so now we have to actually make the call to the MSN search API. The call is made using SOAP, according to our good friend Wikipedia "SOAP is a standard for exchanging XML-based messages over a computer network, normally using HTTP. SOAP forms the foundation layer of the web services stack, providing a basic messaging framework that more abstract layers can build on." 

PHP 5 has SOAP handlers built in (it still must be enabled by uncommenting the relevant line in the PHP.ini file though), however if your using PHP 4 you will need to install an external library to handle communications between your search page and the API server. The one I use is called NuSOAP and is available from http://sourceforge.net/projects/nusoap. The zip file from that URL will contain two folders; lib and samples, upload the lib folder to your server and load nusoap.php from within your search script and your in business. Remember though this step is not required if your using PHP 5.
So here's the next part of the code, it's all PHP this time. The $query value corresponding to the query which is being searched for is in bold, as too are the key which identifies each application to the MSN search API (to get a key visit the MSN Developer Center) and the $resultsRequested variable which holds a number corresponding to the number of results you would like returned. Any comments are in green and will be from now on.
if(isset($_POST[searchSubmitted])) //start if submitted code block
{

require_once('lib/nusoap.php'); //only required if not using PHP 5
$msnsoap = new soapclient("http://soap.search.msn.com/webservices.asmx");
$msnkey = 'E0F4B3633F809FA8F7477CF3D8551BC5C72F4A66'; //enter your key in here
$url = 'www.akamarketing.com'; //enter your URL in here
$query = "site:$url $_POST[searchquery]";
 //build the query which will enable site search on MSN
$numOfResultsRequested = "50";

$params = array(
'AppID' => $msnkey//MSN key which identifies each application to the API server
'Query' => $query//the string variable which the query sent to the MSN API
'CultureInfo' => 'en-US',
'SafeSearch' => 'Off',
'Requests' => array (
'SourceRequest' => array (
'Source' => 'Web',
'Offset' => 0,
'Count' => $numOfResultsRequested//the number of results you want back from the API server 
'ResultFields' => 'All' )
)
);

$msnresults = $msnsoap->call("Search", array("Request"=>$params)); //make the actual call to the API
As you can see (hopefully) it's not rocket science by any means, just plain old computer science. The code first checks that the form has actually been submitted by checking if the $_POST[searchSubmitted] variable exists, because if it does not it means that the user has not searched for anything yet so we of course do not need to make any calls to the MSN search API. Next nusoap.php is loaded as discussed above and then a new SOAP object is created using the API URL.
The last line of code is the actual call to the MSN search API, it uses the $params array to pass in parameters or options for each call. I won't go into all the different options as many would not be of use but the important ones have comments beside them in the code above. So after the call and if everything went right we should have our results in an array called $msnresults.

Displaying the results of the search

 
Now on to the part where the results will actually be outputted. Again it should not be too hard to understand, the only problem might be understanding the structure of the $msnresults array as it's a bit complex and hierarchical, but I will give you the exact code which will give you what you need to access the important data from within the array so you don't even have to look at the structure. If however you would like to see the structure of the array simply uncomment the first line of code below. I'm going to output the results in a very basic format for simplicity, you however can of course style and present the results however you see fit.
//print_r($msnresults);

//store the number of results returned for use in the loop
if($msnresults['Responses']['SourceResponse']['Total'] < $numOfResultsRequested)

{
        $numOfResults = $msnresults['Responses']['SourceResponse']['Total'];
}
else
{
       $numOfResults = $numOfResultsRequested;
}

echo "

";
for($j=0;$j<$numOfResults;$j++)
{

//assign the values to variables to make the final output code cleaner$url = $msnresults["Responses"]["SourceResponse"]["Results"]["Result"][$j]["Url"];
$title = $msnresults["Responses"]["SourceResponse"]["Results"]["Result"][$j]["Title"];
$desc = $msnresults["Responses"]["SourceResponse"]["Results"]["Result"][$j]["Description"];

//table is already created, output
s and ";
echo "
";
echo "
";
echo "";
}
echo "
s
echo "
$title
$desc
$url
 
";
//end if submitted code block
?>



The code is as simple as possible. Initially a variable corresponding to the amount of results returned is created for use in the loop. This variable is created by checking if the total number of results available ($msnresults['Responses']['SourceResponse']['Total']) is less than the number requested. If we decided to use $numOfResultsRequested (in this case 50) to determine how many times our loop should run and there is was only a total of 19 results available/returned well then you'd have a lot of extra s and s with nothing in them taking up space after the 19 results. So it's best to avoid hardcoding wherever possible.
A table to hold the results is then outputted and rows are added to contain the results of the search. I have used the titles as the hyperlink to the URL's of the result and have also outputted the URL below the description too, most of the major search engines use this format. The data is however stored in the $url, $title and $desc variables so you can just output them according to your wishes.

Quick fire instructions to get up a site search with the MSN search API up and running 


Well that's it - a fully functioning site search, hopefully it was not to hard to understand. Don't forget to throw in some validation logic before you use it in a production environment, hey I can't do everything for you :-). I would appreciate all your thoughts and comments on this tutorial, please use the interface below for this. But just to recap the major instructions to get you sorted are:
1. Save all the above code into a file called msnsearch.php on your web server
2. Visit the MSN Developer Center and get an application ID | Insert this into your msnsearch.php file
3. Insert your URL and a number corresponding to the number of results you want into the relevant sections in msnsearch.php
4. Ensure SOAP can be used by either enabling it from the PHP.ini file (PHP 5) or installing & correctly loading NuSOAP from msnsearch.php (PHP 4)

No comments:

Post a Comment

Designed By Blogger Templates