Recently I went looking for a snippet of PHP that provided access to Google's Web Services API, so that I could have a block on my blog which automatically generated google search results based on the page title (or whatever).
I found one, but it didn't work. I fixed it and set it up as a Drupal block; here it is:
// requires PEAR SOAP client
include("SOAP/Client.php");
// Adjust query to suit your purposes.
$sql = "SELECT title FROM node
WHERE type='blog' AND promote=1 AND status=1
ORDER BY created desc limit 1";
$obj = db_fetch_object(db_query($sql));
// first, try and use page title
// note that when reading a blog or story node, the page
// title is the node title.. so
$qstr = drupal_get_title();
// if blank, skip the block
if ($qstr == '')
return; // or apply a default $qstr = 'something';
$output = "Google results for '".$subj."':
";
// Adjust the query prior to SOAP call...
$qstr = $qstr . ' -mydomainname'; // recommend excluding your domain name
$key = 'googleapikey';
$s = new SOAP_Client('http://api.google.com/GoogleSearch.wsdl');
// Adjust further parameters here (see google for docs)
$parameters = array(
'key' => $key, 'q' => $qstr,
'start' => 0, 'maxResults' => 10,
'filter' => false, 'restrict' => '',
'safeSearch' => false, 'lr' => '',
'ie' => '', 'oe' => '');