Using the Google API from PEAR

Jul 1, 12:53 am
tags: , , , ,

[The following excerpt is from Chapter 20 of Pro PHP XML and Web Services by Rob Richards.]

The Services_Google package is simply a wrapper around the SOAP extension and is used to access the Google Web APIs (http://www.google.com/apis/reference.html) for the search engine, spelling suggestions, and cache. To access any of these services, you must obtain a license key by registering for an account. A link to the registration page is available from the previously mentioned URL. Because this package is a wrapper for SOAP, both PHP 5 and the SOAP extension are prerequisites for installing and using this package. Having met these conditions, and because the package is currently in alpha status, you install it using the following:

Bash:
pear install Services_Google-alpha

Once you have installed it, you add support for the package to a script by calling the following:

PHP:
require_once "Services/Google.php";

Note: Google limits you to 1,000 automated queries per day based upon your license key.

This package consists of a single class, Services_Google, that is instantiated by passing your license key as the only argument to the constructor:

PHP:
$google = new Services_Google($key);

Checking Spelling

You can perform a spell check by calling the spellingSuggestion() method. This method takes a string containing the phrase to be checked as its only parameter, and it simply returns a string containing the suggested spelling for the supplied phrase. For example, the following example performs a spell check on the phrase PHP xnl, and it is followed by the resulting phrase suggested by Google’s spell-checking engine:

PHP:
<?php
require_once "Services/Google.php";

/* Google license key */
$key = 'your Google license key';

/* Create instance, passing license key as argument */
$google = new Services_Google($key);

/* Output the resulting suggested spelling */
echo $google->spellingSuggestion('PHP xnl')."\n";
?>
Output:
PHP xml

Retrieving Cached Pages

You can retrieve cached pages from Google by calling the getCachedPage() method from an instantiated Services_Google object. The object takes a single parameter, which is a string containing the URL to retrieve the cached page for, and it returns a string containing the cached page.

Caution: As of version 0.1.1 of this package, this method is incompatible with PHP 5.1 and does not return any valid data.

Searching the Web

You can perform Web searches via a Web service just like using Google from a browser by calling the search() method. Once you have an instantiated the Services_Google object, you can use a number of options, listed below, to control various aspects of the search.

Caution: When running the alpha version 0.1.1 of this package with a Web search, you must always set the limit option, and the start option has no bearing on the results returned.

Query Options
OptionDefault ValueDescription
start0Zero-based index of the first desired result.
maxResults10Maximum number of results to return for the query. The maximum value per query is 10.
limitFALSEAlthough Google limits the maximum number of results for a query, the Services_Google class uses this option to make multiple requests as you move through the returned results, retrieving up to a maximum number of records specified by this option. It is currently mandatory that you use this option, or no results will be returned.
filterTRUEActivates or deactivates automatic results filtering, which hides similar results and results that all come from the same Web host.
restrictsemptyRestricts results to certain countries and/or topics. You can find additional information about using this parameter at http://www.google.ca/apis/reference.html#2_4.
safeSearchTRUEA Boolean used to filter adult content from the results.
languageemptyRestricts the search to documents within one or more languages. Refer to http://www.google.ca/apis/reference.html#2_4 for additional information.

You set these options using the queryOptions property, which is in fact an array where the keys consist of the options from Table 20-1. For example, using $google as the object, you can limit the results to those that are in the French language by using the following convention:

PHP:
$google->queryOptions['language'] = 'lang_fr';
$google->queryOptions['limit'] = 3; /* Set to make the search work */

Once you have set any and all options, the search() method is called, passing the query string as the argument, as shown here. The query string can contain any type of query that is valid to be used on the Google search page itself.

PHP:
$google->search("PHP XML");
foreach($google as $key => $result) {
echo $result->title."\n";
}

You can find additional information about writing advanced queries at http://www.google.ca/apis/reference.html#2_2. The actual results when this is executed look like the following:

Result:
PHPIndex : <b>PHP</b>, <b>XML</b> et XSLT
PHPIndex : <b>PHP</b>, <b>XML</b> et XSLT : point de situation
<b>XML</b> parsing avec <b>PHP</b>


Pro PHP XML and Web Services

The preceding excerpt is from Chapter 20 of Pro PHP XML and Web Services by Rob Richards.

Pro PHP XML and Web Services is the authoritative guide to using the XML features of PHP 5 and PHP 6. No other book covers XML and Web Services in PHP as deeply as this title. The first four chapters introduce the core concepts of XML required for proficiency, and will bring you up to speed on the terminology and key concepts you need to proceed with the rest of the book. Next, the book explores utilizing XML and Web Services with PHP5. Topics include DOM, SimpleXML, SAX, xmlReader, XSLT, RDF, RSS, WDDX, XML-RPC, REST, SOAP, and UDDI.




Add your comments

Please keep your comments relevant to this blog entry: inappropriate or purely promotional comments may be removed. To add hyperlink, please follow this example: "your link text":http://your.link.url