Program Google in PHP

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

PHP (, 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 ( and you have a powerful combination.

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

The Code

Save the following code as a plain text file named googly.php somewhere on your web site where PHP is able to run. Don’t forget to replace insert key here with your Google API key.

# googly.php
# A typical Google Web API php script.
# Usage: Point your browser at googly.php\


<form method="GET">

Query: <input name="query" value="<? print $HTTP_GET_VARS['query'] ?>">
<input type="submit" name="Search">



# Run the search only if you're provided a query to work with.
if ($HTTP_GET_VARS['query']) {

  # Use the NuSOAP php library.
  # 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(""); ...

Get Google Hacks, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.