Presenting a simple search interface requires only a small amount of code, and it turns the previous hard-coded example into your own personal Google. To simplify this (and future) examples, the process of constructing a query has been moved into its own function:
function runGoogleSearch($client, $searchQuery, $start) { $query = array( 'key'=>'u6U/r39QFHK18Qcjz/XdWSbptVaj9k1t', 'q'=>$searchQuery, 'start'=>$start, 'maxResults'=>10, 'filter'=>true, 'restrict'=>'', 'safeSearch'=>true, 'lr'=>'', 'ie'=>'', 'oe'=>'' ); $result = $client->call("doGoogleSearch", $query, "urn:GoogleSearch", "urn:GoogleSearch"); return $result; }
This function
<html> <head> <title>Simple Search Interface</title> </head> <body> <form action=""method="get"> <input type="hidden"name ="start" value="0"> <input type="text" name="query" value=""> <input type="submit"> </form> <br> <?php
This is just your basic HTML form — the hidden variable will be sent to Google to
if (isset($_GET['query'])) {
The code to execute the API call will only be executed if you do in fact have a query to run. This is something to keep in mind because you are only given 1,000 queries per day to play with.
require('../lib/
nusoap
.php');
$searchQuery = $_GET['query'];
$start = $_GET['start'];
The information from the two
GET
variables passed from the form are assigned to their own
$client = newsoapclient ("http://example.org/googleapi/GoogleSearch.wsdl", true); if ($client->getError()) { echo "Error creating client <pre>" . $client->getError() . "</pre>"; exit; } $client->soap_defencoding = 'UTF-8';
Here you check to ensure that the client object was created successfully. You might hit errors at this point if the WSDL file specified in the creation of the object is invalid or unreachable. It is far better to catch things like this now, gracefully, than later when other elements start breaking.
$result = runGoogleSearch(&$client, $searchQuery, $start);
The results are obtained from the new function declared previously. Notice the use of the ampersand before $client ; this is to ensure the object is passed by reference rather than by value. This way if the $client object raises an error, you will be able to check for it later.
{% if main.adsdop %}{% include 'adsenceinline.tpl' %}{% endif %}
if ($client->fault) { echo 'Client Fault<pre>'; print_r($result); echo '</pre>';
The
Server
— Something bad
Client — For some reason the server didn't understand your request. Check your code again and try again.
VersionMismatch
— This happens when the version of SOAP declared in the envelope doesn't match something the server can understand or provide (allows for
MustUnderstand — These errors occur when a header entry includes a MustUnderstand=1 attribute, but the server does not in fact understand the entry. Check the specification for the feed and try again.
} else { if ($client->getError()) { echo 'Error<pre>' . $client->getError() . '</pre>';
A client error
} else { echo "<b>Search Query</b>: <i>" . $result['searchQuery'] . "</i><br>"; $x = $result['startIndex']; $y = $result['endIndex']; if ($result['estimateIsExact']) { echo "Displaying results $x to $y, out of ". $result['estimatedTotalResultsCount'] . "results<br>"; }else { echo "Displaying results $x to $y, out of an estimated ". $result['estimatedTotalResultsCount'] . " results<br>"; } $queryResults = $result['resultElements']; foreach($queryResults as $item) { echo "<a href=\"{$item['URL']}\">{$item['title']}</a><br>"; echo $item['snippet'] . "<br><br>"; } $nextStart = $result['endIndex']; echo "<br><br>"; echo "<a href=\"./nusoap.simple.php?query=$searchQuery&start=$nextStart\">Next 10 Results</a>"; } } } </body></html>
Finally, the results are printed out in some semblance of an attractive manner. Notice the last two
echo
statements near the end — they produce a link to allow the
Figure 6-2