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

Alternatives - 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
It's also possible to write equivalent code using the PEAR SOAP client, rather than the NuSOAP client. The procedure remains largely identical, though there is obviously a difference in the API used. As an illustration, consider the following code segment, which rewrites how the primitive search engine demonstrated a few pages back using the PEAR class:

<html> <head><basefont face="Arial"></head> <body> <?php if (!$_POST['q']) { ?> <h2>Search</h2> <form action="<?=$_SERVER['PHP_SELF']?>" method="post"> Search term: <input type="text" name="q"> </form> <? } else { // include the class include("SOAP/Client.php"); // create a instance of the SOAP client object $soapclient = new SOAP_Client('http://api.google.com/search/beta2'); // 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' => $_POST['q'], // search term 'start' => 0, // start from result n 'maxResults' => 10, // show a total of n results 'filter' => true, // remove similar results 'restrict' => '', // restrict by topic 'safeSearch' => true, // 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"); // print the results of the search if (PEAR::isError($result)) { ?> <h2>Error</h2> <? echo $result['faultstring'];?> <? } else { ?> <h2>Search Results</h2> <? echo $result['estimatedTotalResultsCount'] . " hits found in " . $result['searchTime'] . " ms"; ?> <ul> <? if (is_array($result['resultElements'])) { foreach ($result['resultElements'] as $r) { echo "<li><a href=" . $r['URL'] . ">" . $r['title'] . "</a>"; echo "<br>"; echo $r['snippet'] . "(" . $r['cachedSize'] . ")"; echo "<p>"; } } ?> </ul> <? } } ?> </body> </html>
This is very similar to what you've just seen - not surprising, considering that the PEAR SOAP client is based largely on code taken from Dietrich Ayala's NuSOAP classes. And, in case you want even more convenience, take a look at the SOAP_Google class, also part of the PEAR CVS repository, which provides a set of wrapper functions designed specifically for PHP developers looking to use the Google Web APIs.

Here's a quick example of what a script using the SOAP_Google class might look like:

<html> <head><basefont face="Arial"></head> <body> <?php if (!$_POST['q']) { ?> <h2>Search</h2> <form action="<?=$_SERVER['PHP_SELF']?>" method="post"> Search term: <input type="text" name="q"> </form> <? } else { // include the class include("SOAP_Google.php"); // create a instance of the SOAP_Google client object // pass the license key to the constructor $google = new SOAP_Google('your-google-license-key-xxxxxxxx'); // invoke the search() method $result = $google->search(array('query' => $_POST['q'])); // print the results of the search if ($result) { ?> <h2>Search Results</h2> <? echo $result['estimatedTotalResultsCount'] . " hits found in " . $result['searchTime'] . " ms"; ?> <ul> <? if (is_array($result['resultElements'])) { foreach ($result['resultElements'] as $r) { echo "<li><a href=" . $r['URL'] . ">" . $r['title'] . "</a>"; echo "<br>"; echo $r['snippet'] . "(" . $r['cachedSize'] . ")"; echo "<p>"; } } ?> </ul> <? } else { echo "<h2>Error</h2>"; } } ?> </body> </html>


 
 
>>> 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: