PHPBuilder - An Introduction to Web Services with PHP: Part 2



RSS Twitter
Articles Application Architecture

An Introduction to Web Services with PHP: Part 2

by: Adam Gundry
|
April 19, 2005

At the end of my last article, we had SOAP running with PHP and were using Google as a glorified spell-checker. Seeing as it is rather well known as a search engine, this week we'll look into conducting web searches in a bit more detail. We'll also talk about how to deal with any errors that occur.


Searching with Google

Instead of the simple string returned by doSpellingSuggestion, when calling doGoogleSearch we get a nested structure of objects inside objects. We also need to supply a little more information. Take a look at this, and enter it into a new page in the same folder as the WSDL file.


<dl>
<?php
    $key
= '...';
    require_once
'SOAP/Client.php';
    
$wsdl = new SOAP_WSDL('GoogleSearch.wsdl');
    
$googleProxy = $wsdl->getProxy();

    
$queryResponse = $googleProxy->doGoogleSearch($key, 'PHP', 0, 10, true, '', true, '', '', '');
    
    foreach (
$queryResponse->resultElements as $result)
    {
    PHP article on SOAP Part 2.ems     echo
'<dt><a href="' . $result->URL . '">' . ($result->title ? $result->title : $result->URL) . '</a></dt>';
        echo
'<dd>' . $result->snippet . '</dd>';
    }
?>
</dl>
Now load the page into your web browser. It might take a few seconds for PHP to contact Google, but you should get a list of titled hyperlinks showing the same results as a Google search for 'PHP' (specifically, that's a search with filtering and SafeSearch turned on, and no restrictions in place).


The first half of the code is the same as we saw last time, so let's consider the last part. We call doGoogleSearch to actually do the work and return a results object. The first two arguments are similar to doSpellingSuggestion: a license key and the search query, respectively. There are also a lot of extra parameters on this call that give us the ability to customise the search (apart from the last two, that is, which are just there for decoration). The documentation included with the Google Web APIs developer's kit explains what they do fairly well, so I won't bother repeating it here. One thing to watch for is the data types of the parameters: SOAP is much less forgiving than PHP when it comes to automatically casting types.


Once we've got some search results we will normally want to check for errors, but that step will be added later in this tutorial. For now, we just loop through the main result array contained in the response object and print out a list of links. Aside from a tiny subtlety to deal with the fact that some results might not have titles, that's all we need to do.


Simple error handling

Without proper error handling, however, it's not that great. Change your license key slightly and try again: doesn't look good, does it? Too many PHP developers only code for the normal case and don't take these kind of error conditions into account, but with SOAP it is particularly important to check for errors. You can usually rely on operations to the local server, but when a remote server is involved all sorts of things could go wrong (Google might update the API and remove those two unnecessary arguments to doGoogleSearch, for instance). We can handle most errors simply by inserting this after the doGoogleSearch call:


    if (is_a($queryResponse, 'SOAP_fault'))
        die(
'SOAP fault: ' . $queryResponse->message);
           
    if (
$queryResponse->estimatedTotalResultsCount == 0)
        die(
'No results were returned.'


Now we can search Google and cope with any errors that arise (not in a particularly nice fashion, but I'm sure you can improve that). All we have to do now is decide what to do with this new toy.

1
|
2
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Adam Gundry

Comment:



Comment:

(Maximum characters: 1200). You have characters left.