#native_company# #native_desc#
#native_cta#

An Introduction to Web Services with PHP

By Adam Gundry
on March 16, 2005

Have you ever wanted to enrich your site with information from Google, Amazon, eBay, or one of the many other sites that provide web services through SOAP? In this tutorial, I will be demonstrating how to use the SOAP package from PEAR to query Google’s extensive database (consuming the web service, in the jargon). This tutorial assumes you are using PHP 4 and PEAR::SOAP 0.8.1, and requires basic familiarity with PHP (including a little object-oriented programming).

Note that both Google’s service and PEAR::SOAP are technically still in beta, so you might encounter the odd bug from time to time. There are other SOAP client libraries available, including the PHP5 SOAP extension 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 may also get one or more messages starting with requires package, followed by Dependencies failed. In this case, try the pear install command again but add the dependencies on the command line. For example, if PEAR complained that the Net_DIME package is missing, try using pear install Net_DIME SOAP instead.

Getting a license key

Once you have installed the SOAP module, the next step is to download the Google Web APIs developers’ kit and register to get a license key. This key allows you to make up to 1000 SOAP queries a day free of charge. Visit Google Web APIs and follow the instructions on the site.

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.