In the first part of this series, I showed you how developing applications with the SOAP protocol is a great way to build interoperable software. I also demonstrated how easy it is to build your very own SOAP server and client using the NuSOAP library. This time around I’d like to introduce you to something that you will most definitely run into when working with SOAP – WSDL files.
In this article we’ll talk about what WSDL files are and how to use them. I’ll show you how to quickly build your WSDL files with NuSOAP and incorporate a WSDL file into the SOAP server and client examples from the first part.
What are WSDL Files?
Web Services Description Language (WSDL) files are XML documents that provide metadata for a SOAP service. They contain information about the functions or methods the application makes available and what arguments to use. By making WSDL files available to the consumers of your service, it gives them the definitions they need to send valid requests precisely how you intend them to be. You can think of WSDL files as a complete contract for the application’s communication. If you truly want to make it easy for others to consume your service you will want to incorporate WSDL into your SOAP programming.
Just like SOAP messages, WSDL files have a specific schema to adhere to, and specific elements that must be in place to be valid. Let’s look at the major elements that make up a valid WSDL file and explain their uses.
The root element of the WSDL file is the definitions element. This makes sense, as a WSDL file is by definition a definition of the web service. The types element describes the type of data used, which in the case of WSDL, XML schema is used. Within the messages element, is the definition of the data elements for the service. Each messages element can contain one or morepart elements. The portType element defines the operations that can be performed with your web service and the request response messages that are used. Within the binding element, contains the protocol and data format specification for a particular portType. Finally, we have theservice element which defines a collection of service element contains the URI (location) of the service.
The terminology has changed slightly in naming some of the elements in the WSDL 2.0 specification. portType, for example, has changed its name to Interface. Since support for WSDL 2.0 is weak, I’ve chosen to go over version 1.1 which is more widely used.
Building a WSDL File
WSDL files can be cumbersome to write by hand as they must contain specific tags and are usually quite long. The nice thing about using NuSOAP is that it can create a WSDL file for you! Let’s modify the SOAP server we made in the first article to accommodate this.
Open productlist.php and change it to reflect the code below:
|$server = new soap_server();|
|array(“category” => “xsd:string”),|
|array(“return” => “xsd:string”),|
|“Get a listing of products by category”);|
Basically this is the same code as before but with only a couple of changes. The first change adds a call to configureWSDL(); the method acts as a flag to tell the server to generate a WSDL file for our service. The first argument is the name of the service and the second is the namespace for our service. A discussion of namespaces is really outside the scope of this article, but be aware that although we are not taking advantage of them here, platforms like Apache Axis and .NET do. It’s best to include them to be fully interoperable.
The second change adds additional arguments to the register() method. Breaking it down:
- getProd is the function name
- array(“category” => “xsd:string”) defines the input argument to getProd and its data type
- array(“return” => “xsd:string”) defines the function’s return value and its data type
- urn:productlist defines the namespace
- urn:productlist#getProd defines the SOAP action
- rpc defines the type of call (this could be either rpc or document)
- encoded defines the value for the use attribute (encoded or literal could be used)
- The last parameter is a documentation string that describes what the getProd function does
Now point your browser to http://yourwebroot/productlist.php?wsdl and you’ll see the brand new WSDL file created for you. Go ahead and copy that source and save it as it’s own file called products.wsdl and place it in you web directory.
Consuming WSDL Files with the Client
We’ve modified the SOAP server to generate a WSDL file, so now lets modify the SOAP client to consume it. Open up productlistclient.php created in the previous article and simply change the line that initiates the client from this:
|$client = newnusoap_client(“http://localhost/nusoap/productlist.php“);|
|$client = new nusoap_client(“products.wsdl”, true);|
The second parameter in the nusoap_client() constructor call tells NuSOAP to build a SOAP client to accept the WSDL file. Now launch productlistclient.php in your browser and you should see the same result as before, but now you’re using WSDL power!
In part 2 of this series on creating web services with PHP and SOAP, we went over the importance of using WSDL files for optimum interoperability. We talked about the different elements that make up a WSDL file and their definitions, and then I showed you how to quickly and easily create your own WSDL files with the NuSOAP library. Finally, we modified our SOAP server and client to demonstrate how to use WSDL in your applications.
As you can probably guess, I’ve just barely scraped the surface of what SOAP can do for you, but with these new tools you can provide an easy and well-accepted way of exposing web services to your users.