Build a Site Search with the MSN Search API

Published in Application Programming Interfaces on Thursday, November 17th, 2005

Similar to the Google Search API, MSN uses SOAP to transfer data to and from it's server. Here we have a look at building a site search with the MSN Search API.

Search APIs

The last two articles in this series have looked at the Yahoo! and Google search APIs. The Google and MSN APIs both use SOAP, and the process for acquiring data is quite similar, therefore this instalment will be an overview of using the MSN API, and will look a lot like the Google article.

Getting started with the MSN Search API

Like the other search APIs, MSN provides a development kit from the MSN Search homepage in their development center.

You can apply for an application key from this page by clicking Configure Applications Create and Manage Application IDs. You will need a Microsoft Passport to get your the key.

Our example

As with the other articles, we will be focusing on the Search API to build a site search. We will be taking input, in the form of search terms from a user, and building a request that we will send to the API server. We will then take the response, unserialize it and format the results into some html.

MSN uses the SOAP protocol (specs, W3Schools, Wikipedia) for sending data between it's API server and your application. PHP 5 can handle SOAP natively, however for PHP 4 we need to use an external library to send, receive, and unserialize our communications (sound familiar?). In this example, I've chosen to use NuSOAP. You may also want to check out the PEAR SOAP class.

Step 1: Request and receive

The first thing we need to do is build a request to send to the MSN server. We will do this by setting our search parameters in an array:

Notice that the parameter array is a bit more complex than in the previous two examples. Details about the structure of the request can be found in the help file that comes with the Developer kit.

Site search

In order to make a site search, a couple of things need to take place.

  1. A form that passes the search terms must be used, and when submitted those terms must be passed into the value for Query in our array above.
  2. As MSN doesn't provide a parameter for a site search, a value of must be embedded into the value for q.

Taking into account those two points, the line for Query would be 'Query' => ' search terms'.

Moving forward: request and receive in one easy step

This next part moves quite fast, condensing a few of the steps done in last weeks Yahoo! example into simply a few lines of code. Rather than opening a file with PHP, we will be passing the url and the parameters directly to the nusoap class:

First we include the class, then we instantiate a new 'soapclient', passing it the URI for the API server. From this point we can call the server as outlined in the example.

Nusoap returns the data from the server to us in the form of an array. Compared to the example from last week, this was certainly much simpler to get from the request to an array of data (granted, nusoap is composed of a lot of lines of code, and it did all of the heavy lifting).

A look at the data

If you were to print_r($searchresults) at this point, you would see something similar to the following:

Looking at the array above, we can see that the total number of search results can be taken from $searchresults['Responses']['SourceResponse']['Total'], and that our results are held in $searchresults["Responses"]["SourceResponse"]["Results"]. Each of those elements holds a result, with the associated title, description, url and an MSN cache url. Further down, you can find the search query and the start index, among some other details.

Step 3: Presenting the results

Now that the data is held in an array, it will be quite easy to loop thru the $searchresults["Responses"]["SourceResponse"]["Results"] array and present the data however you see fit. I won't elaborate an example here, but in the downloadable code at the bottom an example is given.

A short discussion

This is the third search API that has been examined, and if you have followed the series, you will notice that this article basically carries over most of the instructions from the Google article. They both use SOAP to communicate, and the only difference can be found in the parameters and the structure of the result array. MSN does offer access to many more queries per day, 10,000 search results per site.

Download some code!

Here is an example script that pulls this whole article together. When using, remember:

  1. To change the extension to php.
  2. That you need a server with php installed to run it.
  3. That you will need an MSN application key to use it.

That's it!

As usual, questions or comments invited below. Next week we move away from search APIs, to something perhaps a little more useful if you happen to have a blog that is more then a year and a half old...

Comments and Feedback

Home » Blog » Web Development » Programming and Scripts » Application Programming Interfaces

Check out the blog categories for older content

The latest from my personal website,

SiteUptime Web Site Monitoring Service

Sitepoint's web devlopment books have helped me out on many occasions both for finding a quick solution to a problem but also to level out my knowlegde in weaker areas (JavaScript, I'm looking at you!). I am recommending the following titles from my bookshelf:

The Principles Of Successful Freelancing

I started freelancing by diving in head first and getting on with it. Many years and a lot of experience later I was still able to take away some gems from this book, and there are plenty I wish I had thought of beforehand. If you are new to freelancing and have a lot of questions (or maybe don't know what questions to ask!) do yourself a favor and at least check out the sample chapters.

The Art & Science Of JavaScript

The author line-up for this book says it all. 7 excellent developers show you how to get your JavaScript coding up to speed with 7 chapters of great theory, code and examples. Metaprogramming with JavaScript (chapter 5 from Dan Webb) really helped me iron out some things I was missing about JavaScript. That said each chapter really helped me to develop my JavaScript skills beyond simple Ajax calls and html insertion with libs like JQuery.

The PHP Anthology: 101 Essential Tips, Tricks & Hacks

Like the other books listed here, this provides a great reference for the PHP developer looking to have the right answers from the right people at their fingertips. I tend to pull this off the shelf when I need to delve into new territory and usually find a workable solution to keep development moving. This only needs to happen once and you recoup the price of the book in time saved from having to develop the solution or find the right pattern for getting the job done..