#native_company# #native_desc#

An Introduction to Web Services with PHP Page 2

By Adam Gundry
on March 16, 2005

Decoding the WSDL file

In the developer’s kit main directory you will find a WSDL file called GoogleSearch.wsdl, which you can open in a text editor or XML browser. This file defines exactly what services we can call using SOAP, though the Google documentation is probably easier to read!

However, we can also see what the SOAP module makes of this file. Create a new PHP page and enter the following.
require_once ‘SOAP/Client.php’;
$wsdl = new SOAP_WSDL(‘GoogleSearch.wsdl’);
header(‘Content-Type: text/plain’);
Place it somewhere on your server along with GoogleSearch.wsdl and access it through your web browser. Assuming everything is working right, you should get a lot of PHP code output. This code is the result of the SOAP_WSDL client class parsing the WSDL file and converting it into PHP functions. This tells us what functions we can call in a rather more readable form than the WSDL document, and is handy particularly if you are using a poorly documented WSDL service.

So how does the code work? Let’s go through it step by step:

  1. First, we load the SOAP client file. If this gives an error, it probably means SOAP isn’t
    installed properly and you should read the instructions above or the PEAR manual.
  2. We then create an instance of the SOAP_WSDL class, based on the GoogleSearch.wsdl file. This is one of
    the main classes we are going to use throughout this tutorial: it parses the WSDL file and represents it as PHP.
  3. Finally, we output the proxy code as plain text. (As you can see from the output, in SOAP
    the proxy is a class that represents the WSDL calls available as PHP functions.)

Spell-checking with Google

If you’ve been using Google for a while (and type as erratically as me), you have probably noticed the “Did you mean…” line that appears if you misspell a word in a search. Thanks to the Google web service, we can add this spelling checker to our PHP applications (though to be honest, it would probably be better to use pspell in an actual application). Enter the following code, with your own license key as the first string.

= ‘Enter your Google APIs key here’;
$wsdl = new SOAP_WSDL(‘GoogleSearch.wsdl’);
$googleProxy = $wsdl->getProxy();
$googleProxy->doSpellingSuggestion($key, ‘diktionary’);
Simple, isn’t it? The first line is trivial and the next two are the same as the previous example. The fourth line creates the proxy object based on the code we looked at earlier, so we can make SOAP requests by calling its methods. Finally, we call doSpellingSuggestion and output the result. Access this page in your web browser, and you should see the word ‘dictionary’ on its own.

You might see ‘Object’ instead, in which case SOAP has encountered a problem and returned a SOAP_Fault object. I will be talking about how to deal with faults later in this tutorial, but for now check you have entered your license key correctly and not managed to exhaust your 1000 query per day limit (!).
To be continued…