As mentioned earlier, one of the advantages of WSF/PHP framework is the ability to use a given service both as a REST style service as well as a SOAP style service.
The good news is that you do not need to change any code in the service script to make it a SOAP service. You can use the same service that we implemented under the service implementation section above and send a SOAP request to the service and receive a SOAP response from the service. It is a feature of WSF/PHP framework for services to respond to clients based on the request format the clients use.
In order to write a SOAP client for the same service we can use the same client code that we used in the above section for implementing REST client and do a few minor modifications.
The first modification is to change the URL slightly. In the REST client, while creating WSClient
, we used the following "to" option.
"to" => "http://localhost/rest/09/library.php/book
"
For the SOAP client, we have to modify this to
"to" => "http://localhost/rest/09/library.php"
Note that we have removed the trailing /book
section from the URL. This is because, while using SOAP, unlike in the case of REST, we do not use the concept of a resource. We just have to use the name of the root service, in this case, library.php
.
The next change is to instruct the client to use the SOAP message format. In the REST client, we used the option:
"useSOAP" => FALSE
For the SOAP client, we could use the option:
"useSOAP" => TRUE
We could also remove this option for the SOAP client because if this option is not present, WSClient
class assumes the SOAP message format by default.
The third and final change required to convert the REST client to a SOAP client with the WSF/PHP is to remove the HTTP method option. In the REST client we used:
"HTTPMethod" => "GET"
In case of SOAP clients and services the usual HTTP method used is POST
. In other words, while using SOAP, the HTTP verb being used is not significant. This is one of the key differences between the SOAP style services and REST style services.
Here is the complete source code for the SOAP client with WSF/PHP for the library service.
<?php $requestPayloadString = <<<XML <getBooks> <book/> </getBooks> XML; try { $client = new WSClient( array("to" => "http://localhost/rest/09/library.php", "useSOAP" => TRUE)); $responseMessage = $client->request($requestPayloadString); printf("Response = %s <br>", htmlspecialchars($responseMessage->str)); } catch (Exception $e) { if ($e instanceof WSFault) { printf("Error String: %s\n", $e->str); printf("HTTP Code : %s\n", $e->httpStatusCode); } else { printf("Message = %s\n",$e->getMessage()); } } ?>
If you use the SOAP client in place of the REST client, you would not see much behaviour difference in the client, in other words, both REST and SOAP clients would give you the same output. However, if you capture the messages that go over the wire while using the SOAP and REST clients and compare them, you will notice a drastic difference in the message formats.
Here is the REST request sent by the REST client.
GET /rest/09/library.php/book HTTP/1.1 User-Agent: Axis2C/1.5.0 Host: localhost
Here is the SOAP request sent by the SOAP client.
POST /rest/09/library.php HTTP/1.1 User-Agent: Axis2C/1.5.0 Content-Length: 177 Content-Type: application/soap+xml;charset=UTF-8 Host: localhost <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"> <soapenv:Header/> <soapenv:Body> <getBooks> <book/> </getBooks> </soapenv:Body></soapenv:Envelope>
As you would immediately notice, the SOAP request is much more bulky than the REST request. This is one of the key criticisms that SOAP gets, and one of the key reasons why people prefer REST over SOAP.
You could notice the same in the responses as well. The response to REST client from the service would look like the following.
HTTP/1.1 200 OK Date: Sun, 28 Sep 2008 02:58:25 GMT Server: Apache/2.2.6 (Win32) mod_ssl/2.2.6 OpenSSL/0.9.8e PHP/5.2.5 X-Powered-By: PHP/5.2.5 Content-Length: 314 Content-Type: text/xml;charset=UTF-8 <books> <book> <id>1</id> <name>Book1</name> <author>Auth1</author> <isbn>ISBN0001</isbn> </book> <book> <id>2</id> <name>Book2</name> <author>Auth2</author> <isbn>ISBN0002</isbn> </book> <book> <id>3</id> <name>Book3</name> <author>Auth3</author> <isbn>ISBN0003</isbn> </book> <book> <id>29</id> <name/> <author/> <isbn/> </book> </books>
The response from the service to the SOAP client would be:
HTTP/1.1 200 OK Date: Sun, 28 Sep 2008 02:59:27 GMT Server: Apache/2.2.6 (Win32) mod_ssl/2.2.6 OpenSSL/0.9.8e PHP/5.2.5 X-Powered-By: PHP/5.2.5 Content-Length: 453 Content-Type: application/soap+xml;charset=UTF-8 <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"> <soapenv:Header/> <soapenv:Body> <books> <book> <id>1</id> <name>Book1</name> <author>Auth1</author> <isbn>ISBN0001</isbn> </book> <book> <id>2</id> <name>Book2</name> <author>Auth2</author> <isbn>ISBN0002</isbn> </book> <book> <id>3</id> <name>Book3</name> <author>Auth3</author> <isbn>ISBN0003</isbn> </book> <book> <id>29</id> <name/> <author/> <isbn/> </book> </books> </soapenv:Body> </soapenv:Envelope>
The difference between SOAP and REST response messages are not as drastic as the request messages, but still, note the wrapping elements that SOAP uses in the response compared to the REST response, that makes even the response message slightly larger.
When the number of interactions increase, the additional overhead in the SOAP messaging style would account considerable overhead. Hence REST style would be the preferred style.
However, there are situations where SOAP is being used in the industry, especially in the enterprise. If you want to sign and encrypt the messages to secure the interactions, and if you want to make the same secure interaction reliable, SOAP has provision for them and the bulky message format comes into use.