n the world of Web development, two technologies have been very, very hot. These are Web services and PHP. The former because they allow big companies with big applications and big ideas to integrate easily; the latter because it is free, easy to use, and easy to learn. Their paths haven’t crossed?until now.
With PHP5, finally, there are native SOAP extensions that allow you to build and consume Web services with this excellent Web development language.
But before going any further, let’s do a quick of Web services and SOAP. One of the biggest challenges facing the Internet generation was the ability to integrate different systems. It was all very nice to have a fancy J2EE application server managing your Web site, pooling connections to Enterprise Java Beans to keep everything running smoothly, but it was no good when you had to write thousands of lines of code to get it to interface to say, a credit card clearing house. Arguably the largest factor contributing to the dot-com crash was the sheer amount of money startups had to spend to hire staff to handle this back-end integration.
This is a problem that Web services were designed to solve. With your services giving data in a standard language (Extensible Markup Language, XML), defined in a standard way (Web Services Description Language, WSDL), located using a standard means (Universal Directory Discovery and Integration, UDDI) and accessed using a standard methodology (Simple Object Access Protocol, SOAP) all you had to do was build one, and everything would work neatly together.
While Web services built by this means are wildly popular, they have tended to exist primarily in the enterprise-level echelon, being used in a .NET or J2EE environment. Small companies and non-commercial Web sites, perhaps scared off by the high cost of infrastructure required to host Web services have gravitated more toward the ASP or PHP model.
With PHP5 you can now have the best of both worlds, as it is fully enabled to develop and consume Web services. In this article I’ll show you how to set up a PHP5-based Web site running under Windows and IIS that consumes standard SOAP-based services.
|Author’s Note: While the source code for this article is designed for PHP5, it should run on any variation of PHP that supports the SOAP extensions and any Web server that supports it.|
|Figure 1. Testing PHP: A simple bit of code will let you test out your first PHP page.|
If you haven’t already done so, download and install PHP5 from the PHP Web site. There you’ll find a few downloads to choose from, including the PHP 5.0.2 “zip package” and the PHP 5.0.2 “installer.” The easiest path to follow is to download the installer and to install that, as it configures the PHP environment for you. For this article I selected IIS as my Web server, and the installer handled plugging PHP into IIS for me. It also handles several other common web servers including Apache. However the installer distribution doesn’t include the SOAP libraries, which have to be downloaded separately.
To get around that, download the PHP zip package , remove the file php_soap.dll, and put it in the C:PHPEXT directory. Then edit the PHP.INI file in the WINDOWS directory to make sure that PHP loads the SOAP extensions. To do this, open PHP.INI in notepad or another text editor and find the ‘dynamic extensions’ area. Underneath this, add the line extension=php_soap.dll. Then, in the ‘extensions and directories’ section, make sure that the extension_dir setting is ‘C:PHPEXT’ (assuming you installed PHP to C:PHP).
|Figure 2. Full Speed Ahead: The SOAP Extensions are working.|
You are now ready to try out PHP, but first it’s good to check if the SOAP extensions are working correctly. Create a directory, phptest, in your Web root directory (C:INETPUBWWWROOT), and put a simple text file, called myfirst.php in there with the following contents:
Then, using your browser, navigate to http://localhost/phptest/myfirst.php. You should see a page like that in Figure 1.
If you scroll this page down a bit, you should see a section called SOAP. If you don’t, you need to go back and check the PHP.INI file again to see if it is loading the extension and that the path is set up correctly. If in doubt, copy all of the extensions from the ZIP file you downloaded earlier to the C:PHPEXT directory and you should be good to go. If SOAP is set up correctly, you should see something like that in Figure 2.
Consuming a Web Service
The nice folks at xmethods.net have provided a lot of Web services to the public, mostly for free, that can be used for building or testing your applications. A great example Web service that you can consume is Xmethods’ Temperatures service, which returns the current temperature at a given ZIP code. You’ll need a very simple client to consume it:
getTemp($ZIP); echo("Temperature is: " . $return);?>
|Figure 3. The Air in Times Square: You can get the current temperature in any ZIP code using PHP SOAP client.|
The simplicity of this code shows you the power of the new SOAP extensions to PHP. In the second line, you create a proxy to the Web service called client, by simply constructing a new client based on the WSDL of the Web service. This is the ‘Web Services Description Language’ in action: This WSDL is enough to create, on the fly, an object that represents locally all the functions of the distant service, and allows you to invoke them as if the distant service was an inline object. The next line does just that?invoking the ‘getTemp’ method of the proxy, passing it the ZIP code that was passed in turn to this page using a URL parameter. Therefore, if you use your browser to save this file in the phptest directory from earlier and call the URL http://localhost/phptest/client.php?zipcode=10036, the browser will render the current temperature in Times Square in New York City (see Figure 3).
Now, building a client is all very nice, but what about building your own Web service in PHP? You’ll be glad to hear that it is just as easy, as you will see in the next section.
Build Your Very Own Web Service with PHP5
In the previous section you built a client that consumed the xmethods Web service that provides temperature information for a specific ZIP code. In this section you will build a simulation of that service, which returns a random number between 40 and 80 instead of the real temperature (interfacing your Web service with the national weather service is a little beyond the scope of this article).
Begin with the following code, a PHP Web service that returns a random number between 40 and 80:
addFunction("getTemp"); $server->handle(); ?>
The code is very simple, but if you try to run it, it won’t work. The reason it won’t work is because it needs to create a new SoapServer based on the WSDL document temp.wsdl, which doesn’t yet exist because you haven’t created it. Just like the SoapClient creates itself based on the Web services definition language, this creates a server as defined in that language.
If you’ve ever worked with WSDL, you’ll know that it is very tricky to write from scratch. A lot of development environments and IDEs generate WSDL automatically for you from your Web service classes. For example, when using Microsoft Visual Studio.NET to create C# Web services, you have a wizard that creates the Web service for you based on a C# class. As you add Web methods to the class, the WSDL is automatically generated by the runtime environment for you.
Using PHP you don’t have this luxury, as you need the WSDL first. Therefore you’ll have to start sharpening your WSDL coding skills to be able to use Web Services in PHP. A nice idea for a new open source project would be a WSDL generator that takes a PHP class and generates WSDL from it!
The WSDL for the temperature Web service is shown in Listing 1.
This isn’t the most intuitive document in the world, but after looking at a few of them you’ll get the hang of what is going on. A great way to start getting used to WSDL is to cut and paste this code, and as you grow your Web service, expand the document as appropriate, describing the new methods that you add.
If you put this WSDL into a text file called temperature.wsdl, and store it in the same directory on your server as the server.php file, you are now ready to run and consume this Web service.
To consume it and prove it works, you will need to amend the Web service client from earlier to point at the new service instead of the xmethods one:
getTemp($ZIP); echo("Temperature is: " . $return);?>
Browsing to this page will give much the same results as earlier, except of course this time the temperature isn’t real, but instead a random number generated by the Web service.
Going on from Here
Web services are an incredibly useful and efficient technology and hopefully you now have the basics to use them with PHP, while letting PHP’s SoapClient and SoapServer classes do most of the grunt work. While the example server itself doesn’t do much, just returning a random number, the sky is the limit for expansion.
One of the great features of PHP is its tight integration with the MySQL database. If you need to secure and control access to your data through a standardized interface, it would be very easy to build upon what you learned here. A Web service that fronts a database and exposes its contents via Web methods is all you would need.
In this article you learned the basics of building and consuming Web services using PHP. This opens up new vistas for PHP coders, who have been restricted before with no native support for SOAP. While there were SOAP libraries available in PHP, only now is it a native part of the language itself, instead of a third party add-on.
Using the SoapServer classes you learned how to build a Web service that exposes your data to standard SOAP interfaces and using the SoapClient class you learned how to consume this and other third-party Web services. All the tools are there in your toolbox to build integratable PHP Web sites. These are the kinds of advancements that will allow PHP to evolve beyond its niche as a cheap alternative for homegrown Web site delivery and into a premier and professional Web application development language.