This is example code of how to query the Google API using Web Services, SOAP, and PHP.
<?
////////////////////////////////////////////////////////////
// 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("<br>An error #" . $ret->getCode() . " occurred!<br>");
print(" Error: " . $ret->getMessage() . "<br>\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<br><pre>");
print_r( $ret );
print("</pre><br>\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.<br>\n");
}
$retry_count++;
}
if( $retry_count == $max_retries )
{
print("<br>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 <a href=\"http://www.google.com/apis/\">Google API home page</a>.<br>\n");
}
return $result;
}
//////////////////////////////////////////////////////////
// The main part of this script
print("<html>\n<head>\n<title>Google API Example with PHP, SOAP, and Web Services</title>\n</head>\n<body>");
print("
<h1>Google API Example Using PHP, SOAP, and Web Services</h1>
<p> </p>
<p>Looking for more info?<br>Please check out the <a href=\"google-api-php-faq.php\">Frequently Asked Questions (FAQs)</a>.</p>
<p>For more info on the Google API, visit the <a href=\"http://www.google.com/apis\">Google developer's page</a></p>
");
$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("<h2>Search results</h2>\n<p>The estimated number of results for the search query <i>$query</i> is <b>$num</b>.</p>");
}
}
//
// print the input form
//
print("<hr><h2>Test out the Google API with PHP, SOAP, and Web Services:</h2><p>
<form method=\"POST\" action=\"apiexample.php\">
<p>Search Query <input type=\"text\" name=\"query\" size=\"20\"></p>
<p>Google Developer's Key <input type=\"text\" name=\"key\" size=\"20\"></p>
<p><input type=\"submit\" value=\"Submit\"><input type=\"reset\" value=\"Reset\"></p>
</form></p>");
print("<hr><h2>About</h2><p>This example was created by <a href=\"http://www.sfu.ca/~gpeters\">Geoff Peters</a>, creator of <a href=\"http://www.googleduel.com/\">GoogleDuel</a> and <a href=\"http://www.googleduel.com/googlebusinessmain.php\">GoogleDuel-Ultra</a>. Feel free to re-use or distribute this source code. Sponsors: <b><a href=\"http://www.foodvancouver.com/\"><b>Vancouver Restaurants</b></a></b>,<a href=\"http://www.gpeters.com/weddings.php\">Vancouver Wedding music</a>, <a href=\"http://www.broadwayflorists.com\">Vancouver flowers</a></p>");
print("</body>\n
</html>");
?>
|