Yahoo Search API

Yahoo searches are a little bit more complex (or simplier?) because there are more options for getting the results and where searches are performed. For this reason we separated them from spell checker, also available from Yahoo.

To perform Yahoo searches you'd need to register an application ID and then pass proper parameters thru REST to search pages located at http://api.search.yahoo.com/WebSearchService/V1/webSearch.

You can found all input & output parameters at http://developer.yahoo.net/search/web/V1/webSearch.html . Basicaly, Yahoo accepts parameters in a single way (REST) and return results in 3 possible ways, at your choice: XML (default), JSON & serialized PHP (based on serialize / unserialize functions from PHP, available since PHP 3.05). Depending on your needs, one or another solution to get results may be more convenient.

You can download from Yahoo website examples for Macromedia Flash, Java, JavaScript, PHP & Python. Server-side, especially in PHP the simplest method is using serialized results (available not only from PHP but other languages too, see this page, like C#, Python, Ruby and more). Here's an example with PHP:

$request =  'http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=YahooDemo&query=something&results=10&output=php';

$response = implode('',file($request));

if ($response === false) {
 die('Request failed');
}

$phpobj = unserialize($response);

foreach($phpobj['ResultSet']['Result'] as $arr) {
  echo "<p><a href='{$arr['Url']}' target='_blank'>{$arr['Title']}</a>\n
           <br>{$arr['Summary']}</p>\n";

Don't forget to replace your application ID and your custom parameters.

JSON version is useful, especially from JavaScript, client-side:

<script language=JavaScript>
function ws_results(obj) {
  var res=obj.ResultSet.Result;
  for (var i = 0;i     document.write("<p><a href='"+res[i].Url+"' target='_blank'>"+
    res[i].Title+"</a>\n<br>"+
    res[i].Summary+"</a></p>\n");
}
</script>

<script type="text/javascript"
src="http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=YahooDemo&query=something&results=10&output=json&callback=ws_results" mce_src="http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=YahooDemo&query=something&results=10&output=json&callback=ws_results">
</script>

It's very important to declare ws_results() callback function before calling Yahoo service. It automatically create an object parameter (obj) based on raw result returned by Yahoo service.

XML examples are provided by Yahoo, so I don't insist on them now.

Yahoo services have a limit of 5000 queries per day per IP. When you hit this limit an error message is returned. You have a log of these searches at http://api.search.yahoo.com/webservices/usage_data

As you can see, there is not very hard to integrare Yahoo search with your websites or applications. For other Yahoo services check our website.

Leave a Comment

You must be logged in to post a comment.