This article covers performing search queries on Google using their API. This means you can transparently run a Google search in the background, and then present the results to your users however you want to. The Google API is a SOAP web service. SOAP (Simple Object Access Protocol) is an XML schema used for calling a remote procedures (usually) over the web. In this case, the remote procedure is Google search.
The instructions on this page will show you how to:
Note: You must eventually register for your own Google developer's key before using this script outside of class.
In this article, we are going to create a single PHP script called google.php, to simply demonstrate how to query Google. Hopefully using this information, you will be able to come up with more advanced ways of using the API. The Google API does offer querying of their cache, as well as spelling requests, however, this article covers only performing searches. If you’re interested in these other types of queries, you’re best of reading the API reference guide.
Download the latest version of the NuSOAP from SourceForge and save the NuSOAP zip file in your public_html
folder on Dante.
lynx -dump http://switch.dl.sourceforge.net/sourceforge/nusoap/nusoap
-0.7.0.zip > nusoap.zip
Unzip the file to expand the contents.
unzip -d nusoap nusoap.zip
Make sure that you have executable permissions for the nusoap classes:
chmod -R +x nusoap
There are a few things you should be aware of before using the Google API, mainly to do with terms and restrictions on the service:
The second limitation (1,000 queries per day) can be overcome by using caching. So whenever you perform a query, save the results to a file, then when somebody performs the same request you can just fetch the results from your hard disk instead of querying Google again. Implementation of this is not covered in this lab, but if you’re interested, PEAR’s Cache_Lite might be a good starting point.
Okay, so the first thing we do is create the parameters. The parameters for NuSOAP are held in associative array, which means the array key is the parameter name, and the corresponding array value is that parameter’s value.
The minimal set of parameters we need are:
There are several more you can specify, but this is enough for our purposes. For a full list, see the Google API reference page.
<?php
$suzi_key = 'fsTYsoZQFHKEO8dMZu1iOo548FgYGD3V';
$q = 'SOME QUERY';
$parameters = array('key' => $suzi_key,
'q' => $q,
'start' => 0,
'maxResults' => 10);
?>
<?php
require_once('./PATH_TO/nusoap.php');
$soap = new SoapClient('http://api.google.com/GoogleSearch.wsdl', 'wsdl');
$result = $soap->call('doGoogleSearch', $parameters);
?>
Now to explain this:
The last thing to do is handle the results. The returned data contains an array called ‘resultElements’, which contain the returned results. So if this array is empty, there were no results. To output them, we simply loop over this array and output each one.
Each result element contains the following data:
Additionally, the returned data contains the following:
<?php
$numResults = count($result['resultElements']);
if ($numResults == 0) {
?>
<p>
Your search yielded no results.
</p>
<?php } else { ?>
<p>
Results <?= $result['startIndex'] ?> to <?= $result['endIndex'] ?>
of <?= $result['estimatedTotalResultsCount'] ?>
</p>
<?php foreach ($result['resultElements'] as $row) { ?>
<h3><a href="<?= $row['URL'] ?>"><?= $row['title'] ?></a></h3>
<p>
<?= $row['snippet'] ?>
</p>
<p>
<?= $row['URL'] ?>
</p>
<?php } ?>
<?php } ?>
We’re going to combine and adapt the code created in previous steps to build our own Google front end. The main changes we will make to our code here involve retrieving the search term from the form, as well as providing the option for returning pages other than just the first.
Remember that this is very basic and there is a lot of further customization you can do.
You can put all this new code in the file google.php
//$q = 'SOME_QUERY'; $q = trim(isset($_GET['query']) ? $_GET['query'] : '');
if (strlen($q) > 0 ) {
$parameters = array('key' => $suzi_key,
'q' => $q,
'start' => 0,
'maxResults' => 10);
// Use the NuSOAP library
require_once('./nusoap-0/lib/nusoap.php');
$soap = new SoapClient($wsdl, 'wsdl');
$result = $soap->call('doGoogleSearch', $parameters);
}
<form method="get"> <input type="text" name='query' value="<?= htmlSpecialChars($q) ?>" /> <input type="submit" value="Submit" /> <input type="reset" value="Reset" /> </form>
<?php
if (strlen($q) > 0) {
printf("Search term: <strong>");
?>
<?= $result['searchQuery'] ?>
<?php
printf("</strong></p>");
$numResults = count($result['resultElements']);
if ($numResults == 0) {
printf("<p>Your search yielded no results. </p>");
} else {
?>
<p>Results <?= $result['startIndex'] ?> to <?= $result['endIndex'] ?>
of <?= $result['estimatedTotalResultsCount'] ?> </p>
<?php foreach ($result['resultElements'] as $row) { ?>
<h3><a href="<?= $row['URL'] ?>"><?= $row['title'] ?></a></h3>
<p><?= $row['snippet'] ?></p>
<p><?= $row['URL'] ?></p>
<?php } ?>
<?php } ?>
<?php } ?>