##Chapter 10 - SOAP Revisited
###Introduction
We will implement our own SOAP clients and servers in this chapter. We will use a PHP SOAP library called NuSOAP (http://www.scottnichol.com/soap) that works with PHP4. Again, PHP5 has much better libraries available, but we are going to use gl which only has the pervious version. This exercise will give us more insight into how distributed systems are put together and the focus is not on programming. You will be given the programs and expected to make only very minor modifications. It will require some effort, however, to understand how the programs work and how all the pieces fit together,
which is our goal. We will see in greater detail how the technology of XML web services fits with implementation technologies.
SOAs that use SOAP-based web services consist of clients and servers that understand SOAP. The back-end processing for the servers must, of course, be implemented in some programming language within a library-
supported framework that will connect the XML of the XML web services to those programs. We will be doing a simple SOA using PHP as the back-end language and the NuSOAP library as the connector piece in the architecture. Since we are only concerned with services to begin with,
we will not use a presentation-oriented framework such as CI until the exercises.
###Installing NuSOAP
NuSOAP is very easy to install. There are instructions in the introduction at the NuSOAP site and I have customized them here for our gl environment.
- Put the zip file of the downloadable release (from SourceForge) in the www directory of your gl account. The easiest way to do this is to use the wget command. You can also scp it up to your account.
- Use the unzip command to unzip the file.
- Rename the long directory name to something shorter such as nusoap.
- It is installed! It really only required copying the files to the server.
You should now read the simple Hello, World tutorial at the NuSOAP site.
We will now go over a different example that uses WSDL.
###Creating a Client in NuSOAP
Listing 10.1 shows a NuSOAP client for the Shakespeare service we used in chapter 4.
You can see the relevant documentation at the NuSOAP site under Programming with NuSOAP Using WSDL but all details are given below. Each program comment is further explained below:
1. The library is included in our program with the built-in require_once function. This function includes a file, but checks to see if it has already been included first. Since the page may be loaded multiple times, this saves steps. Note that the pathname to the library file assumes that this program is in the root of your nusoap directory. You can put it anywhere and update the path.
2. All the parameters for the SOAP must be put into a PHP array. Here we only have one parameter - the string for the line of Shakespeare, but there can be multiple parameters.
3. The NuSOAP library works just like the SoapUI program that we used in chapter 4. It reads the WSDL and creates the SOAP client object automagically from that description. The boolean true parameter just means that we are using WSDL.
4. Now we can use that soapClient object's call method (recall that the arrow notation references an object's methods and properties) with parameter arguments to create the XML SOAP (#5-8 below).
5. Argument1 is the GetSpeech method along with the other parameters to needed to create the XML SOAP.
6. Argument2 is $request which holds the array of parameters for the SOAP message (see #2 above). In this case, it is the string- typed line of Shakespeare.
7. Argument3 is the namespace for the requested web service.
8. Argument4 is the soapaction for the HTTP header.
9. The code is now complete for sending a SOAP message to the Shakespeare web service. The rest of the code is to print out the actual SOAP request and response messages created by NuSOAP and the Shakespeare web service respectively for debugging purposes. Note that the print_r PHP function prints human- readable information about a complex variables so in this case, we do not have to iterate through the array programmatically to see it.
10. The request and response properties of the soapClient object are printed to the browser using the PHP function htmlspecialchars that will convert special characters to HTML entities (such as <) for display. Lookup this function at php.net to see what the ENT_QUOTES parameter does.
So we can see that NuSOAP is an object-oriented library that handles creating an XML SOAP message for us (from the WSDL) and sending it to the web service. We see the request SOAP created by this SOAP client in listing 10.2
PHP NuSoap Shakespeare Client
PHP program to access the Shakespeare web service.
< ?php
require_once('lib/nusoap.php'); // 1. pull in nusoap library
$request = array('Request' => 'Winter of our discontent '); // 2. create an array
$wsdl="http://www.xmlme.com/WSShakespeare.asmx?wsdl";
$soapClient = new soapclient($wsdl, true); // 3. create a soapClient object
$result = $soapClient->call( // 4. use the call method from the soapClient object
'GetSpeech', // 5. the method name from the shakespeare web service
$request, // 6. the Request tag parameter for the method defined above
'http://xmlme.com/WebServices', // 7. the namespace
'http://xmlme.com/WebServices/GetSpeech' // 8. the soapaction
);
echo 'Shakespeare Web Service Response from a PHP Client
';
echo 'PHP Array Response
';
print_r($result); // 9. php function that displays an array variable
// 10. Display the request and response soap with 'htmlspecialchars'
echo 'Request
';
echo '' . htmlspecialchars($soapClient->request, ENT_QUOTES) . '
';
echo 'Response
';
echo '' . htmlspecialchars($soapClient->response, ENT_QUOTES) . '
';
?>
Listing 10.1. The NuSOAP shakespeare client.
POST /WSShakespeare.asmx HTTP/1.0
Host: www.xmlme.com
User-Agent: NuSOAP/0.7.3 (1.114)
Content-Type: text/xml; charset=ISO-8859-1
SOAPAction: "http://xmlme.com/WebServices/GetSpeech"
Content-Length: 475
< ?xml version="1.0" encoding="utf-8"?>
Winter of our discontent
Listing 10.2. The SOAP request.
We should notice a few things about this SOAP request:
- It uses the POST method and the SOAP is contained in the HTTP entity body.
- The HTTP User-Agent is NuSOAP
- The SOAP body uses the namespace for a SOAP encoding. Recall that this is a depreciated way of doing things as RPC/encoded in the WSDL. The Shakespeare service is Document/literal and so notice that the soap encoding namespace is never actually used, it is just boilerplate. A Document/literal web service does require the third argument to call (#7 above - the namespace).
We would need this server-side client for an HTML page we created for a user to access this web service. Recall that a request for a service is subject to the same domain rule for web browsers. A web browser cannot request the Shakespeare web service from the xmlme.com domain when the web page comes from the umbc.edu domain, but using the listing 10.1 program as a proxy client, it will work since server-side programs have no such restriction.
Listing 10.3 shows such an HTML page. You can see working links to all these programs in the on-line syllabus for chapter 10.
HTML form for Shakespeare web service
GetSpeech
GetSpeech requires a string formatted phrase from one of Shakespeare's plays as input. The speech, speaker, and play will be returned as an XML string. Sample Shakespeare Phrases: To be, or not to be | My kingdom for a horse | Get thee to a nunnery | ...
HTML Form for Shakespeare Web Service