Google Hacks Free Open Book

Google Hacks

Previous Section Next Section

Hack 55 Programming the Google Web API with PHP

figs/moderate.giffigs/hack55.gif

A simple example of programming the Google Web API with PHP and the NuSOAP module.

PHP (http://www.php.net/), a recursive acronym for "PHP Hypertext Processing," has seen wide use as the HTML-embedded scripting language for web development. Add to that the NuSOAP PHP module for creating and consuming SOAP-based web services (http://dietrich.ganx4.com/nusoap) and you've a powerful combination.

This hack illustrates basic use of PHP and NuSOAP in concert to interact with the Google Web API.

55.1 The Code

<!--
# googly.php
# A typical Google Web API php script
# Usage: googly.php?query=<query>
-->
<html>
<head>
 <title>googly.php</title>
</head>
<body>
<?
# Use the NuSOAP php library
require_once('nusoap.php');

# Set parameters
$parameters = array(
  'key'=>'insert key here',
  'q' => $HTTP_GET_VARS['query'],
  'start' => '0',
  'maxResults' => '10',
  'filter' => 'false',
  'restrict' => '',
  'safeSearch' => 'false',
  'lr' => '',
  'ie' => 'latin',
  'oe' => 'latin'
);

# Create a new SOAP client, feeding it GoogleSearch.wsdl on Google's site
$soapclient = new soapclient('http://api.google.com/GoogleSearch.wsdl', 'wsdl');

# query Google
$results = $soapclient->call('doGoogleSearch',$parameters);

# Results?
if ( is_array($results['resultElements']) ) {
  print "<p>Your Google query for '" . $HTTP_GET_VARS['query'] . "' found " 
. $results['estimatedTotalResultsCount'] . " results, the top ten of which 
are:</p>";
  foreach ( $results['resultElements'] as $result ) {
    print 
      "<p><a href='" . $result['URL'] . "'>" . 
      ( $result['title'] ? $result['title'] : 'no title' ) .
      "</a><br />" . $result['URL'] . "<br />" .
      ( $result['snippet'] ? $result['snippet'] : 'no snippet' ) .
      "</p>";
  }
}

# No Results
else {
  print "Your Google query for '" . $HTTP_GET_VARS['query'] . "' returned no results";
}
?>
</body>
</html>

55.2 Running the Hack

Invoke this hack from your browser in the same manner you would a CGI script. It accepts one named argument, query with your preferred Google search:

http://localhost/googly.php?query=your google query

55.3 The Results

A search for php looks something like Figure 5-1.

Figure 5-1. PHP results page
figs/gooH_0501.gif
    Previous Section Next Section


         Main Menu
    Main Page
    Table of content
    Copyright
    Dedication
    Credits
    Foreword
    Preface
    Chapter 1. Searching Google
    Chapter 2. Google Special Services and Collections
    Chapter 3. Third-Party Google Services
    Chapter 4. Non-API Google Applications
    Chapter 5. Introducing the Google Web API
    5.1 Hacks #50-59
    5.2 Why an API?
    5.3 Signing Up and Google's Terms
    5.4 The Google Web APIs Developer's Kit
    5.5 Using the Key in a Hack
    5.6 What's WSDL?
    5.7 Understanding the Google API Query
    5.8 Understanding the Google API Response
    Hack 50 Programming the Google Web API with Perl
    Hack 51 Looping Around the 10-Result Limit
    Hack 52 The SOAP::Lite Perl Module
    Hack 53 Plain Old XML, a SOAP::Lite Alternative
    Hack 54 NoXML, Another SOAP::Lite Alternative
    Hack 55 Programming the Google Web API with PHP
    Hack 56 Programming the Google Web API with Java
    Hack 57 Programming the Google Web API with Python
    Hack 58 Programming the Google Web API with C# and .NET
    Hack 59 Programming the Google Web API with VB.NET
    Chapter 6. Google Web API Applications
    Chapter 7. Google Pranks and Games
    Chapter 8. The Webmaster Side of Google
    Colophon
    Index


    More Books
    PHP Hacks
    Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
    The Koran (Holy Qur'an)
    Macromedia Flash 8 Bible
    Search Engine Optimization for Dummies
    YouTube Traffic
    PHP 5 for Dummies
    Harry Potter and The Chamber of Secrets
    Harry Potter and the Sorcerer's Stone
    The Pilgrim's Progress
    Wireless Hacks
    Flash Hacks. 100 Industrial-Strength Tips & Tools
    PayPal Hacks. 100 Industrial-Strength Tips and Tools
    Amazon Hacks
    Pdf Hacks
    The Da Vinci Code
    Google Hacks
    The Holy Bible
    Windows XP For Dummies
    Harry Potter and the Half-Blood Prince
    Seo Book
    Upgrading and Repairing Networks
    Macromedia Dreamweaver 8 UNLEASHED
    Windows XP Annoyances
    Windows XP Hacks
    Microsoft Windows XP Power Toolkit
    Teach Yourself MS Office In 24Hours
    iPod & iTunes Missing Manual
    PC Hacks 100 Industrial-Strength Tips and Tools
    PC Overclocking, Optimization, and Tuning - 2th Edition
    PC Hardware In A Nutshell 3rd Edition
    PC Hardware in a Nutshell, 2nd Edition
    Upgrading and Repairing PCs
    Google for Dummies
    MySQL Cookbook
    Teach Yourself Macromedia Flash 8 In 24 Hours
    PHP CookBook
    Sams Teach Yourself JavaScript in 24 Hours
    PHP5 Manual
    Free Games Paper Airplanes
    500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele