By fx-1 on
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."':<br/><br/>";
// 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' => '');
// Make the SOAP call.
$googleResults = $s->call('doGoogleSearch', $parameters, 'urn:GoogleSearch');
// Handle error and build results list.
if (get_class($googleResults) == 'pear_error') {
$resultMessage = $googleResults->message;
$output .= "<br/>ERROR: $resultMessage<br/>";
} else {
$resultCount = $googleResults->estimatedTotalResultsCount;
$resultElements = $googleResults->resultElements;
$googleHits = '';
if ($count > 0) {
foreach ($resultElements as $item) {
$size = $item->cachedSize;
$title = $item->title;
$url = $item->URL;
$snippet = $item->snippet;
$googleHits .= "<a href=\"$url\" alt=\"$snippet\">$title</a><br/><br/>";
}
}
$output .= $googleHits;
}
return $output;
Comments
Sorry, one line of that examp
Sorry, one line of that example was mangled, it should be:
More info on how SOAP should be configured
I would like to try your block, but I have no experience with SOAP installation.
Could you please help me telling what components I should install under a standard PHP 4.3.4 installation (development on W2K, production on Linux) ??
My installation is:
C:\PHP for PHP
D:\drupal-cvs for Drupal
Which componentsw should I install, and where ???
thanks in advance for your kindness...
Matteo
mailto:webmaster@cantincoro.org
needed extensions
NP -- you'll probably find this useful: http://pear.php.net/ and more directly: http://pear.php.net/packages.php
Lots of info there on accessing and installing PHP extensions.
You need these to use that google block:
SOAP
HTTP
Url
Configure your include_path to point to wherever you put them. /usr/local/lib/php/ is a good default location. You might need to edit your .htaccess in your Drupal install directory, to include that path in include_path.
This is only slightly related
This is only slightly related, but I've been using this script (hosted on our internal site, rather that stealing bandwidth from the author)
http://www.voidstar.com/gnews2rss.php
It generates RSS feeds from news.google.com queries. The feeds work great in Drupal. Quite handy.
Easier to read with highlighted PHP tags
This is the nusoap version
This is the nusoap version of the above block. You can download nusoap from sourceforge.