PHP
  Home arrow PHP arrow Page 7 - 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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Forums Sitemap 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
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? 
Google.com  
PHP

Using The Google Web APIs With PHP
By: Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 31
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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 - Cache Cow
    (Page 7 of 9 )

    Google also allows you to retrieve cached Web pages from its database, via the exposed doGetCachedPage() procedure. Here's a quick example, which demonstrates retrieval of the URL "http://www.xmlphp.com/" from the Google cache.

    <?php // include the class include("nusoap.php"); // create a instance of the SOAP client object $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 'url' => $_GET['u'], // URL to retrieve ); // invoke the method on the server $result = $soapclient->call("doGetCachedPage", $params, "urn:GoogleSearch", "urn:GoogleSearch"); if ($result) { // fault? if (is_array($result) && $result['faultstring']) { // display error echo $result['faultstring']; } else { // decode return value using base64 // and display echo base64_decode($result); } } ?>
    The doGetCachedPage() procedure requires two parameters - your license key, and the URL to be retrieved. This cached content of this URL is retrieved as a base64-encoded data block, which needs to be decoded before it can be displayed. Luckily, PHP comes with a built-in base64_decode() function, which does just that.

    Now, if you pass the script above the URL "http://www.xmlphp.com", like this,

    http://localhost/retrieve.php?u=http://www.xmlphp.com
    here's what you'll see:



    You can link the doGoogleSearch() and doGetCachedPage() procedures to allow Web pages matching your search to be retrieved from the cache, in much the saw way as Google.com does. All that's needed is a slight modification to the search engine script created a couple pages back:

    <? // form submitted // init soap client and call doGoogleSearch // iterate through result foreach ($result['resultElements'] as $r) { echo "<li><a href=" . $r['URL'] . ">" . $r['title'] . "</a>"; echo "<br>"; echo $r['snippet'] . "(" . $r['cachedSize'] . ")"; echo "<br>"; echo "<a href=retrieve.php?u=" . $r['URL'] . ">Get cached page</a>"; echo "<p>"; } // snip ?>
    As you can see, I've added a link below each displayed match to the script "retrieve.php", which is passed the URL to be retrieved from the cache. This URL is sent to the doGetCachedPage() procedure as a procedure argument, and the result is decoded and displayed.{mospagebreak title=Spelling Bee} Finally, Google also allows you to vet spellings, via its doSpellingSuggestion() procedure. Here's a quick example:

    <html> <head><basefont face="Arial"></head> <body> <?php if (!$_POST['p']) { ?> <h2>Spell-check</h2> <form action="<?=$_SERVER['PHP_SELF']?>" method="post"> Check term: <input type="text" name="p"> </form> <? } else { // include the class include("nusoap.php"); // create a instance of the SOAP client object $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 'phrase' => $_POST['p'] // term to check ); // invoke the method on the server $result = $soapclient->call("doSpellingSuggestion", $params, "urn:GoogleSearch", "urn:GoogleSearch"); // print the results of the search if ($result) { if (is_array($result) && $result['faultstring']) { ?> <h2>Error</h2> <? echo $result['faultstring'];?> <? } else { echo "Google suggests <b>" . $result . "</b> for the term <b>" . $_POST['p'] . "</b>"; } } else { echo "No suggestion"; } } ?> </body> </html>
    In this case, the doSpellingSuggestion() procedure expects two arguments - your Google license key, and the term to be checked against Google's internal dictionary. You can use this to build a rudimentary Web-based spell-checker (among other things). This spell-checker is actively used on the Google.com Web site, to identify and warn you about potential spelling errors in your query.

    Here's what the output looks like:





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

       

    PHP ARTICLES

    - Implementing the Iterator SPL Interface
    - Building a Data Access Layer for the Data Ma...
    - Building a Singleton Database with Restricti...
    - Working with Reflected Properties with the R...
    - The Iterator, Countable and ArrayAccess SPL ...
    - Implementing the Data Mapper Design Pattern ...
    - Defining an Abstract Class with Restrictive ...
    - The Reflection API: Working with Reflected M...
    - Using Restrictive Constructors in PHP 5
    - Getting Information on a Reflected Class wit...
    - Introducing the Reflection API in PHP 5
    - Swift Mailer's Batchsend Method and Other Fe...
    - Embedding Attachments into Email Messages wi...
    - Dynamically Attaching Files with Swift Mailer
    - Using Different Paths for Attachments with S...


    Code Analysis Tools
    Enterprise code analysis tools that deliver quality and reliable code



    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek