Archive forMarch, 2006

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.

Comments

Google Search and Spell Checker

Let's start our web API discussion with a first example: Google Search and Spell Checker.

First step is to register a free account. You'll get a key required when using Google functions.

Here's an example how to use it with NuSOAP and PHP:

# Use the NuSOAP php library (provide relative path to include file)
require_once('nusoap.php');

# Set parameters
$parameters = array(
    'key'=>'… your key from Google …',
    'q' => 'keywords for search',
    'start' => 0,
    'maxResults' => '10′,
    'filter' => 'false',
    'restrict' => '',
    'safeSearch' => 'false',
    'lr' => '',
    'ie' => 'latin',
    'oe' => 'latin'
  );

$soapclient =
  new soapclient('http://api.google.com/GoogleSearch.wsdl', 'wsdl');

$results = $soapclient->call('doGoogleSearch',$parameters);

if ( is_array($results['resultElements']) ) {
   echo "<p>Your Google query found "
  . $results['estimatedTotalResultsCount'] . ":</p>";

  foreach ( $results['resultElements'] as $result ) {
    echo "<p><a href='" . $result['URL'] .
      "' target='_blank'>" .
     ( $result['title'] ? $result['title'] : 'no title' ) .
     "</a><br>" . $result['URL'] . "<br>" .
     ( $result['snippet'] ? $result['snippet'] : 'no snippet' ) .
     "<br>".$result['URL']."</p>";
     }
}

This code creates a SOAP client with entry point http://api.google.com/GoogleSearch.wsdl, then call doGoogleSearch() function with imitialized parameters ($parameters). These parameters are straightforward, with the key provided after Google account registration, your keyword keys, start page and the number of results, as well as other parameters.

At the end, you can display results from $results array in any form you want, maybe integrated with other content.

Another useful Google service is online spell checking, available in a similar manner:

# Use the NuSOAP php library
require_once('nusoap.php');

# Set parameters
$parameters = array(
    'key'=>' … your key from Google …',
    'phrase' => 'someting wroong',
    'start' => 0,
    'maxResults' => '10′,
    'filter' => 'false',
    'restrict' => '',
    'safeSearch' => 'false',
    'lr' => '',
    'ie' => 'latin',
    'oe' => 'latin'
  );

$soapclient =
   new soapclient('http://api.google.com/GoogleSearch.wsdl', 'wsdl');

$results = $soapclient->call('doSpellingSuggestion',$parameters);
echo($results);

Notice parameter difference for each service: for searching is used 'q' parameter and for spell checker 'phrase'. Spell-checker phrase is limited to 1024 characters and 10 words.

Google search and spell-checker is limited to 1,000 searches per day. There are no commercial accounts at this time, for more searches. For other details see Google FAQ or API Reference (with parameters description). Also, you can download from Google examples for Java and .NET.

Or see this sample at work (in a popup window).

Other links:

Comments

Welcome to WebAPI.org

Welcome to WebAPI.org

First of all, what's WebAPI.org? It's a free website focused on Internet services available online from different providers, using different technologies and methods. This is a directory of such APIs, with script samples, links and reviews.

More and more companies offer web APIs for useful purposes, in order to expand their client base and enhance other developer applications. For them, it's just another method to bring new visitors or keep satisfied their clients. For users, is a good source of online service, many times for free. For developers, is a challenge to keep their applications up to date with latest technologies and more functionality.

There is not an easy definition of what Web APIs are, and even there are no standards for many solutions or W3C recommendations for them, but we still must know what they are and how to benefit using them. It's like integrating solutions from multiple companies in a dynamic and useful product. Results could be spectacular and can ease our work.

We'll try to offer here details on what a web API offers, how to use it, samples and links.

Web APIs are a very hot and dynamic topic, so, almost every week there are new solutions. We'll try to keep up to date as possible, but still there is no promise. Thanks for helping us with information, correction or comments.

Comments

Next entries » ·