As part of our commitment to the online community through Project Silk Road, we want to make it easy for you to use our API in a way that works best for your needs. The new interfaces exposed by version 2.0 of Live Search API make it very simple to consume results in a variety of environments.
For the near future we plan to use this blog to release reusable code snippets in a variety of environments and languages.
Let’s start with a widely used web programming technology, PHP. From PHP the most straightforward way to call the Live Search API Version 2.0 is to use the API’s JSON interface.
This code snippet shows how to build a basic PHP page that uses the JSON interface to get web search results by using the file_get_contents function to call the interface, and the json_decode function to turn the results into an object that can be processed. In this sample, we use PHP and JSON to send a request to the API’s Web SourceType, but the concept in the sample can be used in any other SourceType or any combination.
In the next blog: PHP and XML
Note: While we have, to this point, focused these samples on Microsoft technology, we are aware that a large number of websites are based on the Linux/Apache/MySQL/PHP (LAMP) stack. While it is very straightforward to use version 2.0 of the API from PHP, a bug in the SOAP extension of PHP in version 5.2.6 and subsequent versions prevented the Live Search API Version 1.1b’s SOAP from being used by a PHP client. This issue has been addressed in Version PHP 5.3.
-- Alessandro
<!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</title>
</head>
<body>
<?php
"Your AppID here";
?>
<form "post" action="<?php echo $PHP_SELF;?>">
<input type="text" id="searchBox" name="searchBox" value="<?php
if (isset($_POST['searchBox'])){
echo($_POST['searchBox']);
}
else {
echo('Microsoft');
}
?>"/>
<input type="submit" value="Search" name="submit" id="searchButton" />
<?php
if (isset($_POST['submit']))
{
$request = 'http://api.search.live.net/json.aspx?Appid=' . $APPID . '&sources=web&query=' . urlencode($_POST["searchBox"]);
$response = file_get_contents($request);
$jsonobj = json_decode($response);
echo('<ul ID="resultList">');
foreach($jsonobj->SearchResponse->Web->Results as $value)
{
echo('<li class="resultlistitem"><a href="' . $value->Url . '">');
echo('<h3>' . $value->Title . '</h3></a>');
echo('<p>' . $value->Description . '</p>');
}
echo("</ul>");
}
?>
</form>
</body>
</html>