Introduction to Web Services With PHP
Note that the service and PEAR both Google:: SOAP are technically still in beta, so you can find the odd bug from time to time. There are other SOAP client libraries available, including the extension PHP5 soap and NuSOAP, and while implementation details are different the basic theory is the same as presented here.
Setting up the PEAR SOAP module
Before we can start using SOAP, we had better make sure both PEAR itself and the SOAP module are available. The PEAR package manager is installed by default with PHP 4.3.0 and later, but if you are using an earlier version or it is missing on your system check out the PEAR manual for instructions.
Once you have the package manager installed, you will need to download the SOAP package. On most systems, you can just enter pear install SOAP into a console window. At least, that’s the theory. Unless there is a stable release of SOAP available, you will get the message No release with state equal to: ‘stable’ found for ‘SOAP’. Try pear install SOAP-beta to get the most current version of the package at the time of writing, even though it is in beta, or tell PEAR to accept beta versions without complaint by typing pear config-set preferred_state beta.
You can also get one or more of the news package is needed first, followed by dependent failures. In this case, try to add the pear install command again, but the dependence on the command line. For example, if the pear complain Net_DIME packet loss, try to use PEAR to install the SOAP instead Net_DIME.
Getting a license key
Once you have installed the SOAP module, the next step is to download the Google Web API developer kit and registration, to obtain the license key. This key can make up for 1000 SOAP queries a day, free of charge. Visit the Google web site API (http://www.google.co.uk/apis/), and in accordance with the site's instructions.
Unzip the developers’ kit and put it in a convenient place. It only contains samples for accessing the service with .NET and Java, but most of the documentation applies equally to calling Google from PHP.
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.
generateProxyCode();
?>
Put it in with you GoogleSearch.wsdl server through your web browser. Suppose all the right to work, you should get a PHP code to a number of outputs. This code is a client-side parsing WSDL file SOAP_WSDL class results, converted to PHP functions. This tells us what we can call the function more readable than the WSDL document, rather than form, is very convenient, especially if you are using a WSDL service records do not.
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 have been using for some time (like me, unstable), Google, you may have noticed the "Did you mean …" OK, if misspell a word appears in the search. On the Google network services, so we can add the spell check our PHP applications (but to be honest, this is likely to be better used in practical applications pspell). Enter the following code, you have a string as the first license key.
gettProxy();
echo $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. We will look into dealing with these faults sometime in the future, but for now check if you have entered your license key correctly and not managed to exhaust your 1000 query per day limit (!).
