PHP
  Home arrow PHP arrow Page 4 - Using The Google Web APIs With PHP
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Using The Google Web APIs With PHP
By: Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 29
    2002-07-16

    Table of Contents:
  • Using The Google Web APIs With PHP
  • Remote Control
  • The Bare Necessities
  • Plugging In
  • Chasing Liberty
  • The Sum Of All Parts
  • Cache Cow
  • Alternatives
  • Closing Time

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Using The Google Web APIs With PHP - Plugging In


    (Page 4 of 9 )

    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


     

       

    PHP ARTICLES

    - Using Aliases and the Autoload Function with...
    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Sub Classing Exceptions in PHP 5
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT