////////////////////////////////////////////////////////////
// This is example code of how to query the Google API using
// Web Services, SOAP, and PHP.
//
// Author: Geoff Peters, January 6th 2004.
//
// To run this script you need to obtain the Pear::SOAP package.
// You can obtain it from http://pear.php.net.
// The example shown uses Pear::SOAP version 0.7.5.
//
// If you don't have administrative access to you web server,
// you can place the SOAP directory that contains the Pear::SOAP
// source files somewhere on your server, and then modify the
// PHP include path using ini_set to include this directory.
//
// For example:
/*
// set the include path to use the new pear stuff
ini_set( 'include_path', '/var/www/vhosts/gpeters.com/ultra:/usr/share/pear:.');
*/
// Note that Pear::SOAP has several dependencies on other Pear packages,
// which you should also install on your web server.
/////////////////////////////////////////////////////////////
//
// Initialize SOAP web services
//
include("SOAP/Client.php");
$soapclient = new SOAP_Client('http://api.google.com/search/beta2');
$soapoptions = array('namespace' => '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("
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("The estimated number of results for the search query $query is $num.
"); } } // // print the input form // print(""); print("
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("