There are currently three major ways to build a Web service with PHP. I say, “major” because there’s really almost an infinite number of ways to do it–but most are based on one of three methods: SOAP, XML-RPC and REST.
The preferred method of communication for Web services is the SOAP protocol, but there’s nothing built into PHP that handles this protocol for you. That means you either have to use a third-party tool for adding SOAP functionality, or build one yourself. I checked out two tools for adding SOAP functionality to PHP–nuSOAP and the Pear SOAP package–and they both make for some remarkably simple coding.
Caveats: The catch, with both packages, is that they are still beta releases. You may find them useful and stable, but the folks who built them are not yet that confident. Also, the installation documentation for each references various tweaks you must make to your server, and dependency packages that must be installed prior to using SOAP.
These little snags put PHP Web services out of the running for cutting-edge application development, pushing them on past the bleeding-edge of research and development and into the nether realms of “maybe someday” development. But that’s not necessarily a bad thing. Both packages are distributed under Open Source licensing so you can modify the code for stability and performance within your own, particular site.
With nuSOAP a coder can enable the consumption of an existing Web service by including the webservice.php file. Then, to actually consume the service, he would reference the WSDL file by calling the remote method.
Listing A is an example of the three (yes, 3!) lines of code you need for consuming a Web service with nuSOAP.
The other package covered here is the Pear SOAP package–another third-party tool for adding SOAP functionality to PHP.
With Pear SOAP, a coder can enable the consumption of an existing Web service by including the Client.php file. Then, to actually consume the service he would:
- Create the SOAP client
- Specify pertinent options
- Call the remote method.
If you install the entire Pear SOAP package on your server, it will create a directory called example. Listing B is an example of the code used to consume a Web service with the Pear SOAP package. It uses the sample scripts from the distribution archive.
The code in Listing B will fill the variable $sEquation with the word problem, and $sResult with the answer.
XML-RPC is becoming a much more popular Web services communication medium than SOAP. This is particularly so in multi-server environments because XML-RPC is good about enabling the use of various components, distributed across multiple servers.
Much like with SOAP, XML-RPC is not built-in to the current stable release of PHP. Unlike SOAP, it can be implemented without too much server-tweaking. You can download the Pear XML-RPC package from PHP.Net. It consists of two files, Server.PHP and RPC.php.
With Pear SOAP, a coder can enable the consumption of an existing Web service by requiring the RPC.php file. Then, to actually consume the service, he would:
- Define the function parameters
- Define the function
- Call the function with the defined parameters
- Parse the response
Listing C is an example of the code used to consume a Web service with the Pear XML-RPC package.
Amazon.com and a number of other smaller organizations are making good use of Representational State Transfer (REST) as an alternative to SOAP. REST calls are made over HTTP, and parameters are specified in the query string. The result is that, on both the client and the server, there’s no need to get busy in the HTTP headers.
An example of a Web service request would look something like Listing D.
When the Web page MyWebService.php receives this request, it will respond with an XML file (or just about any other appropriate document). The coder can then go ahead and process that file however he likes–probably by using XSLT to format it into HTML for display in a browser.
Beyond the code
Looking beyond the code for a moment, the question you may have to answer is “Which one should YOU use?” The answer is also a question, “Which one fits your project?”
With SOAP calls, you get ubiquity. Most programmers, when considering Web services, assume that there will be SOAP transactions involved. That means that, although Web services may support other protocols, they should almost all support SOAP requests.
With XML-RPC calls, you get performance. Not only can service functions be distributed amongst several servers, but the code behind them is much simpler to write. That means that development goes quickly, operations run quickly, and revisions come easily.
With REST calls, you get control. You don’t rely on the server software to manage the header data and you don’t rely on third-party utilities to manage the communications.
In all cases, Web services will require a high degree of focus on security measures. Badly coded applications could result in corrupt or malicious data entering your system. Worse yet, authentication and authorization requirements are heavily reliant upon network administration issues, rather than on the Web service systems themselves.
This article exposed you to three methods for invoking Web services through PHP scripting. Third-party utilities like nuSOAP and the Pear SOAP module allow PHP to invoke Web services using the SOAP protocol. The Pear XML-RPC package allows PHP to invoke Web services using the XML-RPC protocol. The REST protocol is more of a traditional, HTML type of Web service–requiring no third-party utilities.