Location: PHPKode > scripts > clsGoogleApi > clsgoogleapi/clsgoogleapi.php
// include the class nusoap. This class can be obtained from http://dietrich.ganx4.com/nusoap/index.php
// Once downloaded, put it in somewhere in your site tree, and change the next line to reflect that


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

class clsGoogleApi {

	// These properties are used in the class:
	var $theResultSet; // holds the results of the search as given by google api
	var $theResults=array(); //holds the results, and is intended to do the traversing
	var $theRowShown=0; // internal field. Holds the index to the last row shown
	var $theMaxResults; // internal field. Holds the given max results parameter to the constructor
	var $flgError = false; // indicates if was there error or not
	var $theSearchQuery; // the Search query as returned by Google Api
	var $theEstimatedResultsCount; // The number of results found by the Api

	function clsGoogleApi($search_what,$start,$maxResults) {

		global $soapclient;
		$params = array(
			 'key' => 'tbZF6P1QFHJB23E5zqVwgyytmQPuVkLC',   // Google license key This is a valid license. But get your own license, by going to www.google.com/api
			 'q'   => $search_what,                         // search term
			 'start' => $start,                             // start from result n
			 'maxResults' => $maxResults,                   // 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
		$this->theResultSet=$soapclient->call("doGoogleSearch", $params, "urn:GoogleSearch", "urn:GoogleSearch");

		// print the results of the search
		if ($this->theResultSet['faultstring']) {
			echo $this->theResultSet['faultstring']."<br>";
		} else  {
			if (is_array($this->theResultSet['resultElements'])) {
				foreach ($this->theResultSet['resultElements'] as $r) {
					$result["directory category"]=$r['directoryCategory'];
					$result["related information present"]=$r['relatedInformationPresent'];
					$result["directory title"]=$r['DirectoryTitle'];
	function getResultNextItem() {
		if (($this->theRowShown > $this->theMaxResults) or ($this->theRowShown > $this->theEstimatedResultsCount))  {
		return $result;

This is an example on how to use the class.

  $myQuery=new clsGoogleApi("michael jackson",0,25); // Search for Michael Jackos, starting on the first found record, and getting a max of 25 items

    if ($myQuery->flgError) { // if error found do something
        echo "Error!";
    } else {
		echo "Search of ". $myQuery->theSearchQuery." got ".$myQuery->theEstimatedResultsCount." results<hr>";
		echo "<ul>";
        while ($result=$myQuery->getResultNextItem()) {
			echo "<li> $item - ".$result["title"]." (".$result["URL"].")<br>".$result["snippet"]."(".$result["cached-size"].")";
		echo "</ul>";
Return current item: clsGoogleApi