MSN Live Search API V2.0 in PHP

At the end of 2008, Microsoft abandoned their old search API. While older applications continue to work, new AppID’s only work with the new V2.0 API. If you’ve created a new AppID, and wonder why none of the code samples you find on the Web work with it, now you know. It’s not hard to update code to use the newer (still in beta) V2.0 API, and we’ll do exactly that by updating our previous API search code. If you’ve used the old code, it’s quite simple to update to the new code. Just copy the file, and change one line in your PHP page — the new code requires PHP5, so be sure your server has PHP5 installed.

Quick Update

Get the new Search API V2.0 PHP class and copy it to the same location as the older code. Find the line in your PHP script reading include ("MSNSearch.php") and replace it with include ("MSNSearchV2.php"). You’ll then need to get a new AppID from Microsoft and update your page (V1.0 AppID’s don’t work with V2.0 API, and vice-versa).

How it works

Examine the source code for the class yourself — it formats a HTTP URL and sends it to Microsoft’s server, receiving the search response as XML. It then uses PHP’s SimpleXML to parse the results (only in PHP5, which is why PHP5 is required). The class has a few helper functions to assist so you don’t have to write search forms or format results, and CSS classes tag all HTML code for easy formatting.

Microsoft’s new V2.0 API’s don’t require SOAP (although you can use it if you want), so the previous requirement of a SOAP library disappears. The new V2.0 API’s have the option to send requests as HTTP (returning results formatted as XML) without using SOAP.

To get a functioning search page, place this PHP code snippet in a script and you’ll have a ready-made search page, with a few customizations for your specific site.

  1. Place the Search API V2.0 PHP class in the same directory as your script (or change the include line to point it to the location of where you placed it).
  2. Place your AppID key instead of “INSERTAPIKEYHERE”.
  3. If you don’t have your script on your webserver as /search-msn.php, change the definition of SEARCH_URL.
<?php
define("SEARCH_URL","/search-msn.php");
include("MSNSearchV2.php");

# Values from HTTP GET, or reasonable defaults
$start = isset($_GET['start']) ? intval($_GET['start']) : 1;
$q = isset($_GET['q']) ? $_GET['q'] : "";
if ($start < 1)
    $start = 1;

print searchform($q,"form_top",SEARCH_URL);
if (strlen($q) > 1) {
  $msnsearch = new MSNSearch('INSERTAPIKEYHERE');
  $sresult = false;
  $msnsearch->setQuery($q);
  $msnsearch->setPage($start);
  $sresult = $msnsearch->search();
  if (($sresult === true) && ($msnsearch->totalRecords > 0)) {
    print $msnsearch->search_header($q);
    print $msnsearch->search_results();
    print $msnsearch->search_navagation($q);
    print searchform($q,"form_bottom",SEARCH_URL);
  } else 
    print "<p>Sorry, no results found for <b>$q</b>.</p>n";
}
?>

All PHP code on this page licensed under a BSD license.

References:

Stay up to date with new articles and content. It’s free, and we won’t sell your information. SIGN UP TODAY by entering your primary email address below to guarantee you won’t miss anything.