Blog
RSS feed

PHP and XML

We continue now with the second in this series of samples demonstrating the use of Live Search API, Version 2.0 Beta with PHP.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Live Search API 2.0 through PHP and XML</title>
</head>
<body>
<?php
$APPID = "Your AppID here";
>
<form method="post" action="<?php echo $PHP_SELF;?>">
<input type="text" id="searchBox" name="searchBox" value="<?php
        if (isset($_POST['searchBox'])){
         echo($_POST['searchBox']);
        }
    ?>"/>
    <input type="submit" value="Search" name="submit" id="searchButton" />
    <?php
    if (isset($_POST['submit']))
    {
     $request = 'http://api.search.live.net/xml.aspx?Appid=' . $APPID . '&sources=web&query=' .
urlencode( $_POST["searchBox"]);
     $response = new DOMDocument();
                 //echo $request . "<br/>";
     $response->load($request);
                 $webResults = $response->getElementsByTagName("WebResult");
     if ($webResults->length<>0)
     {
           echo('<ul ID="resultList">');                   
               foreach($webResults as $value)
               {
                  /*
                      Sample response from Live Searh XML API for Web:
                      <?xml version="1.0" encoding="utf-8" ?>
                      <?pageview_candidate ?>
                      <SearchResponse xmlns="http://schemas.microsoft.com/LiveSearch/2008/04/XML/element" Version="2.0">
                      <Query>
                           <SearchTerms>microsoft</SearchTerms>
                      </Query>
                      <web:Web xmlns:web="http://schemas.microsoft.com/LiveSearch/2008/04/XML/web">
                           <web:Total>255000000</web:Total>
                           <web:Offset>0</web:Offset>
                           <web:Results>
                           <web:WebResult>
                           <web:Title>Microsoft Corporation</web:Title>
                           <web:Description>Get product information, support, and news from Microsoft. ...
Microsoft Silverlight delivers a new generation of high-quality audio and video, engaging media experiences,
and ...</web:Description>
                           <web:Url>http://www.microsoft.com/</web:Url>
                           <web:DisplayUrl>http://www.microsoft.com/</web:DisplayUrl>
                           <web:DateTime>2008-11-07T08:51:18Z</web:DateTime>
                           </web:WebResult>
                           </web:Results>
                           </web:Web>
                           </SearchResponse>
                               The interesting fields (Title, Description and Url are at position 0,1 and 2 as children
of WebResult.
                               The API result is strongly typed and the order of these elements is guaranteed                                
                  */
                     echo('<li class="resultlistitem"><a href="' . $value->childNodes->item(2)->nodeValue . '">'); //Url
                     echo('<h3>' . $value->childNodes->item(0)->nodeValue . '</h3></a>'); //Title
                     echo('<p>' . $value->childNodes->item(1)->nodeValue . '</p>'); //Description
                }
                echo("</ul>");
             }

             else
             {
                     echo "<p>No results for query</p><b>" . $_POST["searchBox"] . "</b><p>please modify your query
and try again</p>";
             }

     }
     ?>
</form>
</body>
</html>
Published Thursday, January 08, 2009 1:00 PM by ianwh