'urn:GoogleSearch', 'trace' => 0); //////////////////////////////////////////////////////////// // Calls the Google API and retrieves the estimated number of // search results for that query into $num. function do_search( $query, $key, &$num ) { global $soapclient; global $soapoptions; // Note that we pass in an array of parameters into the Google search. // The parameters array has to be passed by reference. // The parameters are well documented in the developer's kit on the // Google site http://www.google.com/apis $params = array( 'key' => $key, // the Developer's key 'q' => $query, // the search query 'start' => 0, // the point in the search results should Google start 'maxResults' => 10, // the number of search results (max 10) 'filter' => false, // should the results be filtered? 'restrict' => '', 'safeSearch' => false, 'lr' => '', 'ie' => '', 'oe' => '' ); // Here's where we actually call Google using SOAP. // doGoogleSearch is the name of the remote procedure call. $ret = $soapclient->call('doGoogleSearch', $params, $soapoptions); if (PEAR::isError($ret)) { print("
An error #" . $ret->getCode() . " occurred!
"); print(" Error: " . $ret->getMessage() . "
\n"); return false; } else // We have proper search results { // Results from the Google search are stored in the object $ret. // The following block of code prints // out the structure and contents of the object to the screen: print("\n
");
		print_r( $ret );
		print("

\n"); // in this example, the only thing we need from the search results // is the estimatedTotalResultsCount $num = $ret->estimatedTotalResultsCount; } return true; } //////////////////////////////////////////////// // Does Google search with retry. // Retry is useful because sometimes the connection will // fail for some reason but will succeed when retried. function search( $query, $key, &$num ) { $result = false; $max_retries = 5; $retry_count = 0; while( !$result && $retry_count < $max_retries ) { $result = do_search( $query, $key, $num ); if( !$result ) { print( "Attempt $retry_count failed.
\n"); } $retry_count++; } if( $retry_count == $max_retries ) { print("
Sorry, connection to Google failed after retrying several times. Please check that the Google Developer's Key you entered was correct. To obtain a developer's key or for more information on the Google API, visit Google API home page.
\n"); } return $result; } ////////////////////////////////////////////////////////// // The main part of this script print("\n\nGoogle API Example with PHP, SOAP, and Web Services\n\n"); print("

Google API Example Using PHP, SOAP, and Web Services

 

Looking for more info?
Please check out the Frequently Asked Questions (FAQs).

For more info on the Google API, visit the Google developer's page

"); $key = $_REQUEST['key']; $query = $_REQUEST['query']; if ( $key == "" ) { /* You get a developer's key when you register to use Google's API. A developer's key is a unique string that identifies you to Google. You get a maximum of 1000 searches per day using your developer's key. */ $key = 'sampleBQFHLJLmAUSgnSiZySpQmfd1wqG'; // put your developer's key here. } if( $query != "" ) { // remove the slashes that are automatically added by PHP before each quotation mark $query = stripslashes($query); if( search( $query, $key, $num ) ) { print("

Search results

\n

The estimated number of results for the search query $query is $num.

"); } } // // print the input form // print("

Test out the Google API with PHP, SOAP, and Web Services:

Search Query

Google Developer's Key

"); print("

About

This example was created by Geoff Peters, creator of GoogleDuel and GoogleDuel-Ultra. Feel free to re-use or distribute this source code. Sponsors: Vancouver Restaurants,Vancouver Wedding music, Vancouver flowers

"); print("

Source Code



"); print("

Download the source code

"); highlight_file("apiexample.txt"); print("\n "); ?>