Home arrow PHP arrow Page 4 - Using The Google Web APIs With PHP

Plugging In - PHP

The Google Web APIs allow developers to build SOAP-basedapplications driven off Google's unique indexing and searchcapabilities. And since there are now quite a few PHP classes designedfor SOAP transactions over HTTP, integrating the two has never beensimpler. This article explains how.

TABLE OF CONTENTS:
  1. Using The Google Web APIs With PHP
  2. Remote Control
  3. The Bare Necessities
  4. Plugging In
  5. Chasing Liberty
  6. The Sum Of All Parts
  7. Cache Cow
  8. Alternatives
  9. Closing Time
By: Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 31
July 16, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
Google has made three methods available in their Web API. Here's what they look like:

doGoogleSearch() - search for a specified term in the Google database;

doGetCachedPage() - retrieve a page from the Google cache;

doSpellingSuggestion() - retrieve a spelling suggestion from Google.

This might not seem like much, but you'll soon see that it's more than enough to build some fairly powerful applications. Take a look at the following example, which provides a gentle introduction to running a search query on Google with PHP:

<?php // include the class include("nusoap.php"); // create a instance of the SOAP client object // remember that this script is the client, // accessing the web service provided by Google $soapclient = new soapclient("http://api.google.com/search/beta2"); // uncomment the next line to see debug messages // $soapclient->debug_flag = 1; // set up an array containing input parameters to be // passed to the remote procedure $params = array( 'key' => 'your-google-license-key-xxxxxxxx', // Google license key 'q' => 'melonfire', // search term 'start' => 0, // start from result n 'maxResults' => 10, // show a total of n results 'filter' => false, // remove similar results 'restrict' => '', // restrict by topic 'safeSearch' => false, // remove adult links 'lr' => '', // restrict by language 'ie' => '', // input encoding 'oe' => '' // output encoding ); // invoke the method on the server $result = $soapclient->call("doGoogleSearch", $params, "urn:GoogleSearch", "urn:GoogleSearch"); // print the results of the search print_r($result); ?>
The first order of business is to include the SOAP class which contains all the methods needed to access SOAP services. I've used NuSOAP here, but I'll show you the same thing using the PEAR class a little further down.

<?php // include the class include("nusoap.php"); ?>
Now, in this SOAP universe, Google provides the SOAP server, and this PHP script works as the client. So, the next step is to instantiate this client, using the class constructs provided by NuSOAP.

<?php // create a instance of the SOAP client object $soapclient = new soapclient("http://api.google.com/search/beta2"); ?>
The class constructor accepts a single parameter, which is the URL of the SOAP service to be accessed (this is sometimes referred to by geeks as the "endpoint").

All that remains is to send a request to the SOAP server - something easily accomplished by the class' call() method.

<?php // invoke the method on the server $result = $soapclient->call("doGoogleSearch", $params, "urn:GoogleSearch", "urn:GoogleSearch"); ?>
The call() method accepts four arguments - the name of the remote procedure to be invoked, an array containing arguments for this remote procedure, a method namespace and a SOAPAction value. The remote procedure name here is doGoogleSearch(), and the list of arguments to be passed to it are stored in the $params array, which looks like this:

<?php // set up an array containing input parameters to be // passed to the remote procedure $params = array( 'key' => 'your-google-license-key-xxxxxxxx', // Google license key 'q' => 'melonfire', // search term 'start' => 0, // start from result n 'maxResults' => 10, // show a total of n results 'filter' => false, // remove similar results 'restrict' => '', // restrict by topic 'safeSearch' => false, // remove adult links 'lr' => '', // restrict by language 'ie' => '', // input encoding 'oe' => '' // output encoding ); ?>
Here's what those arguments mean:

"key" - your Google license key;

"q" - the query string;

"start" - the index number of the first result to display;

"maxResults" - the maximum number of matches to display (limited to a maximum of 10);

"filter" - a Boolean indicating whether or not to display matches which are similar to each other;

"restrict" - limit the search to a specific section of the Google database;

"safeSearch" - filter out adult content from the result set;

"lr" - limit the search to a specific language;

"ie" - the character encoding of the query string;

"oe" - the character encoding of the result set.

If you take a look at the internals of the SOAP class, you'll see that the call() method uses the arguments passed to it to generate a SOAP request, which looks something like this:

POST /search/beta2 HTTP/1.0 User-Agent: NuSOAP v0.6 Host: api.google.com Content-Type: text/xml Content-Length: 946 SOAPAction: "urn:GoogleSearch" <?xml version="1.0"?> <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:si="http://soapinterop.org/xsd" xmlns:galactivism="urn:GoogleSearch"> <SOAP-ENV:Body> <galactivism:doGoogleSearch> <key xsi:type="xsd:string">your-google-license-key-xxxxxxxx</key> <q xsi:type="xsd:string">melonfire</q> <start xsi:type="xsd:int">0</start> <maxResults xsi:type="xsd:int">10</maxResults> <filter xsi:type="xsd:boolean">0</filter> <restrict xsi:type="xsd:string"></restrict> <safeSearch xsi:type="xsd:boolean">0</safeSearch> <lr xsi:type="xsd:string"></lr> <ie xsi:type="xsd:string"></ie> <oe xsi:type="xsd:string"></oe> </galactivism:doGoogleSearch> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
This request packet is transmitted to the SOAP server using the POST method, and a server response packet is transmitted back to the client. Here's what one such packet might look like:

HTTP/1.0 200 OK Date: Fri, 21 Jun 2002 05:51:46 GMT Content-Length: 10920 Content-Type: text/xml; charset=utf-8 Server: e h c a p a Via: 1.1 HathCache (NetCache NetApp/5.0.1R2D3) <?xml version='1.0' encoding='UTF-8'?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:xsd="http://www.w3.org/1999/XMLSchema"> <SOAP-ENV:Body> <ns1:doGoogleSearchResponse xmlns:ns1="urn:GoogleSearch" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <return xsi:type="ns1:GoogleSearchResult"> <documentFiltering xsi:type="xsd:boolean">false</documentFiltering> <estimatedTotalResultsCount xsi:type="xsd:int">3880</estimatedTotalResultsCount> <directoryCategories xmlns:ns2="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns2:Array" ns2:arrayType="ns1:DirectoryCategory[0]"> </directoryCategories> <searchTime xsi:type="xsd:double">0.247468</searchTime> <resultElements xmlns:ns3="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns3:Array" ns3:arrayType="ns1:ResultElement[10]"> <item xsi:type="ns1:ResultElement"> <cachedSize xsi:type="xsd:string">6k</cachedSize> <hostName xsi:type="xsd:string"></hostName> <snippet xsi:type="xsd:string"> &lt;b&gt;...&lt;/b&gt; Check out. what we&apos;ve cooked up in our brand-new PHP section. Copyright 1998-2002&lt;br&gt; &lt;b&gt;Melonfire&lt;/b&gt;. All rights reserved Terms and Conditions | Feedback. </snippet> <directoryCategory xsi:type="ns1:DirectoryCategory"> <specialEncoding xsi:type="xsd:string"></specialEncoding> <fullViewableName xsi:type="xsd:string">Top/Computers/Internet/Web_Design_and_Development/ Desi gners/M</fullViewableName> </directoryCategory> <relatedInformationPresent xsi:type="xsd:boolean">true</relatedInformationPresent> <directoryTitle xsi:type="xsd:string">&lt;b&gt;Melonfire&lt;/b&gt; </directoryTitle> <summary xsi:type="xsd:string">A production house with business divisions focusing on content production and Web development. </summary> <URL xsi:type="xsd:string">http://www.melonfire.com/</URL> <title xsi:type="xsd:string">&lt;b&gt;melonfire&lt;/b&gt;!</title> </item> <item xsi:type="ns1:ResultElement"> <cachedSize xsi:type="xsd:string">11k</cachedSize> <hostName xsi:type="xsd:string"></hostName> <snippet xsi:type="xsd:string"> &lt;b&gt;...&lt;/b&gt; Copyright 1998-2002 &lt;b&gt;Melonfire&lt;/b&gt;. All rights reserved&lt;br&gt; Terms and Conditions | Feedback. </snippet> <directoryCategory xsi:type="ns1:DirectoryCategory"> <specialEncoding xsi:type="xsd:string"></specialEncoding> <fullViewableName xsi:type="xsd:string"></fullViewableName> </directoryCategory> <relatedInformationPresent xsi:type="xsd:boolean">true</relatedInformationPresent> <directoryTitle xsi:type="xsd:string"></directoryTitle> <summary xsi:type="xsd:string"></summary> <URL xsi:type="xsd:string">http://www.melonfire.com/community/columns/trog/ar chiv es.php?category=PHP</URL> <title xsi:type="xsd:string">The &lt;b&gt;Melonfire&lt;/b&gt; Community - Trog</title> </item> -- snip -- </resultElements> <endIndex xsi:type="xsd:int">10</endIndex> <searchTips xsi:type="xsd:string"></searchTips> <searchComments xsi:type="xsd:string"></searchComments> <startIndex xsi:type="xsd:int">1</startIndex> <estimateIsExact xsi:type="xsd:boolean">false</estimateIsExact> <searchQuery xsi:type="xsd:string">melonfire</searchQuery> </return> </ns1:doGoogleSearchResponse> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
This response packet is decoded by the client into a native PHP structure, which can be used within the script. At the moment, all I'm doing is printing it - and here's what the output looks like:

Array ( [documentFiltering] => false [estimatedTotalResultsCount] => 3880 [directoryCategories] => [searchTime] => 0.05159 [resultElements] => Array ( [0] => Array ( [cachedSize] => 6k [hostName] => [snippet] => <b>...</b> Check out. what we've cooked up in our brand-new PHP section. Copyright 1998-2002<br> <b>Melonfire</b>. All rights reserved Terms and Conditions | Feedback. [directoryCategory] => Array ( [specialEncoding] => [fullViewableName] => Top/Computers/Internet/Web_Design_and_Development/Designers/M ) [relatedInformationPresent] => true [directoryTitle] => <b>Melonfire</b> [summary] => A production house with business divisions focusing on content production and Web development. [URL] => http://www.melonfire.com/ [title] => <b>melonfire</b>! ) [1] => Array ( [cachedSize] => 11k [hostName] => [snippet] => <b>...</b> Copyright 1998-2002 <b>Melonfire</b>. All rights reserved<br> Terms and Conditions | Feedback. [directoryCategory] => Array ( [specialEncoding] => [fullViewableName] => ) [relatedInformationPresent] => true [directoryTitle] => [summary] => [URL] => http://www.melonfire.com/community/columns/trog/archives.php?category=PH P [title] => The <b>Melonfire</b> Community - Trog ) // remaining array elements snipped out // ) [endIndex] => 10 [searchTips] => [searchComments] => [startIndex] => 1 [estimateIsExact] => false [searchQuery] => melonfire )
Obviously, this is not very useful - but we're just getting started. Flip the page, and I'll show you how to massage all this raw data into something resembling a search results page.

 
 
>>> More PHP Articles          >>> More By Harish Kamath, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: