Monday, March 10, 2014

How To Generate WSDL for PHP SOAP Server

This is a simple tutorial on how to generate WSDL file for PHP SoapServer class.

For this tutorial to work, you need to enable SOAP in your PHP configuration. If not, you will get this error message if you try to run this tutorial without soap enabled.

Fatal error: Class 'SoapServer' not found in...

Just google around on how to enable soap in php.


PHP 5 SOAP Server class support both WSDL and Non WSDL mode. In WSDL mode, you typically pass a WSDL file as a parameter to the SoapServer class. eg, 

Assuming our server class is in http://localhost/example/server-test.php, we can call http://localhost/example/server-test.php?wsdl to access the WSDL we passed as a parameter to the PHP SoapServer.

In non WSDL mode, we pass NULL to the PHP Server class and set the uri option to the target namespace for the server. eg

Calling http://localhost/example/server-test.php?wsdl, this time around, will result in the error below, because the SoapServer Class in PHP 5.3.5 does not support automatic WSDL generation;

This means that if we need the WSDL from our PHP SOAP Server (as in the case of some clients that need to create a reference to our PHP class/functions), we have to find a way to create it and pass it a parameter to the PHP SOAP Server.


To setup our PHP Server class to generate WSDL automatically, we can either use the Zend Framework or NuSOAP. In this tutorial, we will focus on how to create the WSDL file manually. The only disadvantage of using this method is that you have to modify the WSDL file whenever you add a new function or change a method signature in your PHP code. So, it is better to write your PHP SOAP Server functions before creating the WSDL.
We create a class, ExampleClass, with two functions, boom and getDate. The boom function takes two parameters (an associative array with two keys), first and last. It retrieves the values and returns the concatenated string. The getDate function simply returns the current server date.


Creating a WSDL file from scratch can be very complicated and error prone. To ease this process, we will use an IDE such as Netbeans to create the WSDL file for our PHP functions. We are not going to write any code in Netbeans, but simply use the IDE to define our PHP functions so that Netbeans can generate the WSDL file for us.

1.    1.    Open Netbeans and go to File -> New Project -> Java Web -> Web Application

     2. Click on Next and enter the project name

     3.       Click next on the remaining dialog boxes .

4.       Right-click the ExampleClass node and choose New -> Web Service.

5.       Enter, ExampleClassWS as the Web service name and type org.example.wsdl in Package. Ensure that, Create Web Service from Scratch is selected. Click on finish.

6.       To create our functions in the WSDL, navigate to the design view window shown below

 7.       Click on Add Operation in the visual designer

8.      Enter the function name, boom. Leave the “Return Type” as String. As earlier said, the function, boom, is expecting two parameters, first and last. Click on “Add” on the dialog box to enter these values. Leave the parameter “Type” as String.

9  .     Click OK to exit the dialog box and return to the editor.

10. Repeat step 7 and 8 to create the second function

-> Delete the default hello() method in the source code to remove the hello operation.

11. Go to the Projects tab in the IDE, expand the Web Services node of the ExampleClass project. Right-click the ExampleClassWS node, and choose Generate and Copy WSDL...

12. Specify where you want to store the WSDL file. In our example, the WSDL file will be stored in the root folder of our project, ExampleClass. Click Ok to generate the WSDL file.

13. Go to  netbeans project directory -> ExampleClass, to see the generated WSDL file.

This is all you need to generate the WSDL file for your PHP SOAP Server class.

Copy both ExampleClassWS.wsdl and ExampleClassWS_schema1.xsd, to the location of your PHP SOAP Server e.g http://localhost/example/. Implement your PHP SOAP Server in WSDL mode, passing this WSDL file as a parameter. You can view the complete SOAP Server codes here. And check out the PHP SOAP Client tutorial to see how we consumed this Web Service. 

Find below the generated WSDL file. 
A sample SOAP request and response obtained from calling the boom() method is also shown below;





  1. broken link in Click here to see the generated WSDL file.

  2. @Anonymous
    The broken link has been fixed.

    Thanks for the observation.

  3. ExampleClassWS_schema1.xsd Missing

  4. And what if you want to introduce complex types?

    1. Please see the link below on how to add complex type to the header. But this is also applicable to the body too.


Related Posts Plugin for WordPress, Blogger...