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:

Leave a Comment

You must be logged in to post a comment.