SOAP Server and Client, now with WSDL part 2

Now here is the part where I give the example files. After we have planned the application functions, we need to figure out what we are going to return to the client. This is going to be a standard array with at least two levels: The Response array and the Data array. It will look similar to this:

Array
(
    [responseMsg] => Array
        (
            [status] => ok
            [message] => Service call was successful
        )
    [allColors] => Array
        (
            [0] => blue
            [1] => green
            [2] => black
            [3] => white
            [4] => yellow
            [5] => red
            [6] => beige
        )
)

Now that we have the basic idea, we need to create the WSDL file. Remember, it is very important to think of WSDL files as of reading from bottom to top. The final WSDl file is located here. Here is the basic idea of the WSDL file I created (going from the bottom to the top):

  1. Service: This houses the binding, the location, the port, and the name.
  2. Binding: This houses the functions that will be exposed, the operation and the input and output encoding. Most of the time these will be similar with only the names being different.
  3. Port Type: Here is where I define the operations and the input/output definitions
  4. Message: These are individual nodes for the Request and Response messages for all functions. These will usually have two message nodes per function, and they will define the structure for each action
  5. Types: This defines each structure that has been mentioned in the Message and any subsequent structures that have to be defined. This is usually the area where most struggles occur.
    • This structure will be encompassed by schema target namespace
    • Import the XML namespaces to help build the structures in the reasponse
    • For each complex type, it should either mention a specific data type (xsd:), or a new defined structure (tns:)
    • Each structure that is an array should be defined as a SOAP-ENC:Array with a wsdl:arrayType parameter

So that is the WSDL. The one I have created defines the 2 functions, the input, the output, and the structure of each. Now we can move on to the Server code.

Using PHP SOAP can be as easy as you make it or as hard as you make it. First off, we need to define the functions in the file. I called my server file, server.php. I want to set the cache to null, and then define those functions.

<?php 
ini_set('soap.wsdl_cache_enabled', false);

function getColors($which = null){
. . . 
}

function getNames($set = null){
. . . 
}

The next part it to get the server going. Hopefully, before doing this you have read the PHP manual online and read the parts about the SOAP server, and you will find this to be fairly easy. First we need to instantiate the new Server class, and pass to it the URL for the WSDL as a parameter. This is what I am doing for this example, but there are other ways and parameters that one could use. Next, we need to add the functions to the class, so that we can expose those out to the clients. Finally, we need to call the function to handle the calls from the client. Although this may sound a bit much, here is the code to get it done, with this example:

// Set up the PHP SOAP server
$server = new SoapServer("http://www.hirdweb.com/soap/hirdweb.wsdl");
// Add the exposable functions
$server->addFunction("getColors");
$server->addFunction("getNames");
// Handle the request 
$server->handle();

And just like that, you have the server set up. What I am not showing here is the code for the functions. These are basic, and at the end of this post, will be the link to the code files. Basically each function takes the static array, and returns the data to the client. There is one extra function that I put in that build the Response header array. The getNames function just does a SWITCH statement to check to see which part of the static array to return.

Now to the client. Again, another easy implementation. First, we are not going to cache the WSDL. We are going to set up the client and point to the WSDL, setting trace to true in case we need the last response and request for debugging. I am also going to show which functions are available, so I will print that out. Next we need to call the functions, and set parameters if needed. So the code for this is again fairly easy.

// Set to disable caching of the wsdl file
ini_set('soap.wsdl_cache_enabled', false);

// Create the SOAP client
$client = new SoapClient("hirdweb.wsdl", array('trace' => 1));

// Show all possible functions that can be used
show($client->__getFunctions()); 

// Make a call to the getColors method and print out the results
$info = $client->__soapCall("getColors", array());
show($info); 

// Make a call to the getNames method and print out the results
$names = $client->__soapCall("getNames", array("set" => "all"));
show($names); 

And the client is done. I created another function here called “show” which just wraps the array in “pre” tags. And now the client is complete. Make sure you test your WSDL file, and for complete compatibility, check the webservice in something like SoapUI and build a quick C# console app to make sure that can read from it. Once that happens, your webservice should be able to talk to any app, even if it does not use PHP on the back end. So have fun coding and building these webservices.

All Code Files and Examples, WSDL, etc

This Post Has 3 Comments

Leave a Reply