PHP and XML

  • Comments (14)

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>
Join Bing Community
  • Useful information for me, just finished a little library of bing written in php and this gave me another idea :)

    Also, PLEASE! either use something with overflow: auto in css or get a syntaxhighlighter or anything, its so horrible!...

  • This chunk of code has saved me a lot of headache and trial and error. But since Bing has arrived, do we change every reference to "LiveSearch" in the code to "Bing"? That's when referencing the schema.

  • This has saved me a headache too.

  • this php and xml example has saved me loads of time for my hotels and travel website, thank you.

  • @quality directory i think change code to bing and not live search

  • quote: this php and xml example has saved me loads of time for my hotels and travel website, thank you.

    saved me alot of time too

  • Do you have any code samples that would be easy to incorporate into WordPress or better yet a plugin?

    David

  • I love Bing Search so Much!

  • Thanks for sharing this code.  I'm glad that there is a huge support for PHP over there at Bing, it's very clean and straight forward.

  • thank you for this code

  • Ok, this works fine, but search is limited to 10 search results! How can i do, that will show all founded results in same page?

  • Very useful, I like it

  • Thanks. could you put a simple demo of a page where you use this script.

  • http://bedehbeih.blogspot.com/

    emailgratisanyail.blogspot.com

Page 1 of 1 (14 items)