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.WSDL Structure
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.<definitions>
<types>
........
</types>
<message>
<part></part>
</message>
<portType>
.......
</portType>
<binding>
....
</binding>
<service>
....
</service>
</definitions>
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 more part
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 the service
element which defines a collection of 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. Openproductlist.php
and change it to reflect the code below:
<?php
require_once "nusoap.php";
function getProd($category) {
if ($category == "books") {
return join(",", array(
"The WordPress Anthology",
"PHP Master: Write Cutting Edge Code",
"Build Your Own Website the Right Way"));
}
else {
return "No products listed under that category";
}
}
$server = new soap_server();
$server->configureWSDL("productlist", "urn:productlist");
$server->register("getProd",
array("category" => "xsd:string"),
array("return" => "xsd:string"),
"urn:productlist",
"urn:productlist#getProd",
"rpc",
"encoded",
"Get a listing of products by category");
$server->service($HTTP_RAW_POST_DATA);
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 namearray("category" => "xsd:string")
defines the input argument togetProd
and its data typearray("return" => "xsd:string")
defines the function’s return value and its data typeurn:productlist
defines the namespaceurn:productlist#getProd
defines the SOAP actionrpc
defines the type of call (this could be eitherrpc
ordocument
)encoded
defines the value for theuse
attribute (encoded
orliteral
could be used)- The last parameter is a documentation string that describes what the
getProd
function does
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 upproductlistclient.php
created in the previous article and simply change the line that initiates the client from this:
$client = new nusoap_client("http://localhost/nusoap/productlist.php");
to this:
$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!
Summary
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. Comments on this article are closed. Have a question about PHP? Why not ask it on our forums? Image via Lilyana Vynogradova / ShutterstockFrequently Asked Questions (FAQs) about Web Services with PHP and SOAP
What is the role of WSDL in SOAP-based web services?
WSDL, or Web Services Description Language, plays a crucial role in SOAP-based web services. It is an XML-based interface definition language that is used for describing the functionality offered by a web service. In simpler terms, it is like a contract that defines what the web service does and how it should be called. It includes details like the methods the service exposes, the format of the input and output messages, the transport protocol to be used (usually HTTP), and the location of the service.
How can I handle SOAP faults in PHP?
SOAP faults are errors that occur during the processing of a SOAP message. In PHP, you can handle SOAP faults using the try-catch block. When a SOAP fault occurs, the PHP SOAP extension throws a SoapFault exception that you can catch and handle appropriately. The SoapFault exception provides methods to get the fault code, fault string, and other details about the fault.
What is the difference between SOAPClient and SOAPServer in PHP?
In PHP, SOAPClient and SOAPServer are two classes provided by the SOAP extension. SOAPClient is used to send requests to a SOAP-based web service. It provides methods to call the service methods and handle the responses. On the other hand, SOAPServer is used to create a SOAP-based web service. It provides methods to define the service methods and handle incoming requests.
How can I use SOAP headers in PHP?
SOAP headers are optional elements in a SOAP message that can be used to provide additional information that is not part of the payload. In PHP, you can use the SoapHeader class to create SOAP headers. You can add the SoapHeader object to the SOAP message using the __setSoapHeaders method of the SOAPClient class.
How can I debug SOAP requests and responses in PHP?
Debugging SOAP requests and responses in PHP can be done using the built-in features of the SOAP extension. The SOAPClient class provides the __getLastRequest and __getLastResponse methods that return the last SOAP request and response as XML strings. You can use these methods to inspect the SOAP messages and identify any issues.
How can I handle complex types in SOAP with PHP?
Handling complex types in SOAP with PHP can be done using the classmap option of the SOAPClient class. The classmap option allows you to map WSDL types to PHP classes. This way, when a complex type is received in a SOAP response, it is automatically converted to the corresponding PHP object.
What is the role of namespaces in SOAP?
Namespaces in SOAP are used to avoid element name conflicts. They are similar to packages in programming languages. A namespace is defined using a URI, and each element in a SOAP message can be associated with a namespace.
How can I use SOAP with HTTPS in PHP?
Using SOAP with HTTPS in PHP requires setting up the stream context with the appropriate SSL options. This can be done using the stream_context_create function and the stream_context option of the SOAPClient class.
How can I handle attachments in SOAP with PHP?
Handling attachments in SOAP with PHP can be done using the SOAP extension’s support for MIME and DIME attachments. You can add attachments to a SOAP message using the addAttachment method of the SOAPClient class.
How can I handle SOAP version mismatches in PHP?
SOAP version mismatches in PHP can be handled by specifying the SOAP version in the options array when creating a SOAPClient or SOAPServer object. The SOAP extension supports both SOAP 1.1 and SOAP 1.2, and you can switch between them as needed.
Stephen Thorpe is originally from London but now living in Tennessee. He works at an Internet and Telephone company as an applications developer primarily using PHP and MySQL.