[thelist] Google Web API: PHP implementation
Simon Willison
simon at incutio.com
Fri Apr 12 06:25:01 CDT 2002
I've been playing around with the Google Web API, PHP and SOAP all
morning and I finally got something working. I'd post a public demo of
it but developers only get 1,000 queries a day on my license key and
I've used up a lot of mine already.
Cheers,
Simon Willion
=================================================
<?php
/* Accessing the Google Web API via PHP
- by Simon Willison (simon at incutio.com)
This code is in the public domain - do whatever you want with it
To use this code you will need both PEAR and the PEAR SOAP package
installed somewhere on your php include path. You can get the SOAP
package from CVS:
cvs -d :pserver:cvsread at cvs.php.net:/repository login
(enter phpfi as the password)
cvs -d :pserver:cvsread at cvs.php.net:/repository co pear/SOAP
You will need a Google license key - see this site:
http://www.google.com/apis/
There is a strange bug (I think) in the SOAP package. Some search
results run with the following code return a PEAR error object
announcing "XML error on line X: not well-formed (invalid token)"
I have not traced the error fully but it seems to originate from
the XML parser. The XML returned by Google is well formed and I am
not sure of the reason for the parser error. One example search
term which demonstrates this behaviour is "php".
*/
include("SOAP/Client.php");
// Google search query
$query = 'soap';
// Your google license key
$key = 'xxxxxxxxxxxxxxxxxxxxxxxxx';
$s = new SOAP_Client('http://api.google.com/search/beta2');
$result = $s->call('doGoogleSearch', array(
'key' => $key,
'q' => $q,
'start' => 0,
'maxResults' => 10,
'filter' => false,
'restrict' => '',
'safeSearch' => false,
'lr' => '',
'ie' => '',
'oe' => '',
), 'urn:GoogleSearch');
// Is result a PEAR_Error?
if (get_class($result) == 'pear_error')
{
$message = $result->message;
$output = "An error occured: $message<p>";
}
else
{
// We have proper search results
$num = $result['estimatedTotalResultsCount'];
$elements = $result['resultElements'];
$list = '';
if ($num > 0) {
foreach ($elements as $item) {
$size = $item['cachedSize'];
$title = $item['title'];
$url = $item['URL'];
$snippet = $item['snippet'];
$desc = "<p><b>$title</b> - <a href=\"$url\">$url</a>
<small>[Size: $size]</small></p>";
$desc .= "\n<blockquote>$snippet</blockquote>\n\n";
$list .= $desc;
}
}
$output = "$num results returned:\n\n$list";
}
echo $output;
?>
=================================================
More information about the thelist
mailing list