Archive forSearch

Digg API

Fianally Digg launched their API :

The Digg Application Programming Interface (API) has been created to let users and partners interact programmatically with Digg. The API returns Digg data in a form that can be easily integrated into an application or a web site.

See the documentation at http://apidoc.digg.com/

Here's some interesting parts from their docs:

Be Polite, Please!

Use good judgment when designing your application. Don't make repeated requests for the same data frequently. […] We monitor API usage, and we may block applications that do silly things.

They also launched a contest for using this API.

Comments

Google AdSense API

Google has just opened for all developers their AdSense API introduced last year:

If you're a web developer or host, you may remember that we introduced you to the AdSense API last May. With the AdSense API, your users can create their own AdSense accounts on your site, and display ads alongside the content that they have created. They can also manage their accounts and view ad performance and earnings reports, all on your site.

Today, we're pleased to announce that the AdSense API is now open for all developers, with the release of our open development sandbox. This is a replica of the live service with some additional support to help you test and debug your applications. Once you implement the AdSense API in the development sandbox, we can go live with your implementation.

Using this API you can share advertising revenues with creating content online users without webnasters worrying too much about accounting and money, all managed by Google. It looks liek a win-win situation for everybody.

Some already fear that AdSense API will skyrocket click-fraud. However this is a sample account creatin using PHP straight from their online documentation:

<?php

// Copyright 2005, Google Inc. All rights reserved.

/**
 *  sample code to create an Adsense account through Adsense API
 */

require_once('lib/nusoap.php');
require_once('common.php');

$server = 'https://www.google.com';
$namespace = 'http://www.google.com/api/adsense/v2';

// Set up the authentication headers
$email = '<impl:developer_email>REPLACE WITH DEVELOPER EMAIL</impl:developer_email>';
$password = '<impl:developer_password>REPLACE WITH DEVELOPER PASSWORD</impl:developer_password>';
$client_id = '<impl:client_id>NOT RELEVANT</impl:client_id>';

$header = $email . $password . $client_id;

// creating soap client
$wsdl = $server . '/api/adsense/v2/AccountService?wsdl';
$client = new soapclient($wsdl, true);
$err = $client->getError();
if ($err) {
  showSoapClientError($err);
  return;
}
$client->soap_defencoding = 'UTF-8';

// Set the headers; they are needed for authentication
$client->setHeaders($header);
if ($client->fault) {
  showMyErrors($client);
  return;
}
$err = $client->getError();
if ($err) {
  showSoapClientError($err);
  return;
}

// setting the parameter
$param = '<loginEmail>users_address_here@example.com</loginEmail>';
$param .= '<entityType>Individual</entityType>';
$param .= '<websiteUrl>http://test.aaa.com</websiteUrl>';
$param .= '<websiteLocale>en</websiteLocale>';
$param .= '<usersPreferredLocale>en_US</usersPreferredLocale>';
$param .= '<emailPromotionPreferences>true</emailPromotionPreferences>';
$param .= '<synServiceTypes>ContentAds</synServiceTypes>';
$param .= '<hasAcceptedTCs>true</hasAcceptedTCs>';
$param = '<createAdSenseAccount>' . $param . '</createAdSenseAccount>';

// invoke web service
showCall('createAdSenseAccount', $param);
$response = $client->call('createAdSenseAccount', $param, $namespace);
if ($client->fault) {
  showMyErrors($client);
  return;
}
$err = $client->getError();
if ($err) {
  showSoapClientError($err);
  return;
}

// get back the response
$response = $response['createAdSenseAccountReturn'];
showCreatedPublisher($response);

// showing the soap
showRequestResponse($client);

?>

Comments

MSN Search API

Like other search engines, MSN made public a few APIs, including the most important: web search. They are using SOAP and could be used online from any other programming language and platforms.

As usual, first step is registration. You have to register your free application ID, needed in your web requests. MSN provided some MSDN help files and samples, but only for C# & VB.NET.

Our example here will be in PHP with NuSOAP. See it at work in this small demo (popup window). The PHP source code is this (only for MSN interrogation part):

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

# Set parameters
$parameters = array(
  'AppID'=>'…your AppID…',
  'Query' => $_POST['key'],
  'CultureInfo' => 'en-US',
  'SafeSearch' => 'Off',
  'Requests' => array (
     'SourceRequest' => array (
         'Source' => 'Web',
         'Offset' => 0,
         'Count' => 10,
         'ResultFields'  => 'All' )
      )
   );

$soapclient =
    new soapclient('http://soap.search.msn.com/webservices.asmx');

$results = $soapclient->call('Search',array("Request"=>$parameters));

if( is_array($results['Responses']) ) {
  echo "<p><b>Your MSN query for '" . $_POST['key'] . "' found "
 . $results['Responses']['SourceResponse']['Total'] . ":</b></p>";
  foreach ( $results['Responses']['SourceResponse']['Results'] as $result ) {
 echo "<p><a href='" . $result['Url'] .
  "' target='_blank'>".$result['Title'].
  "</a><br />".$result['Description'].
  "<br />". $result['DisplayUrl']."</p>";
  }
 }
}

Don't forget to replace your AppID.

As you can see, the URL for web service is http://soap.search.msn.com/webservices.asmx and function name is 'Search'. You have to build proper parameters before calling function, most important 'Query', 'Source' (for web or local search), 'Offset' and 'Count' (how many results).

Results are returned as associative arrays, containing other arrays, for instance $results['Responses']['SourceResponse']['Results']['Url']. Hint: to see all fields, replace displaying part with a simple

print_r($results).

Usage limit is quite large: up to 10,000 queries a day. For more options see MSDN documentation.

Comments

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