Published on WSO2 Oxygen Tank (http://wso2.org)

PHP Web Services: Messaging – SOAP and REST

By samisa
Created 2008-02-24 00:44

In this article by Samisa Abeysinghe, he discusses various features available in the WSO2 Web Services Framework for PHP that can be used to send and receive messages to and from services and clients. He discusses how to deal with SOAP messages as well as REST style invocations.

 

Applies To

WSO2 WSF/PHP 1.2.0
Environment Windows or Linux

 

Table of Content

  1. Background [0]
  2. SOAP Versions [0]
  3. Rest [0]
  4. SOAP Faults [0]
  5. Summary [0]

 

PHP Web Services: After Getting Started [0]”, I introduced an example that dealt with calculating a factorial, and showed how to trace SOAP messages, using the getLastRequest() and getLastResponse() methods of the WSClient [0] class. However, I did not discuss in detail aspects like how to identify the current SOAP versions in use and how to use different SOAP versions. Also, I did not describe how to check if there was a SOAP fault and in case there was one, how to deal with it. In this article I aim to deal with those areas. So, let's dig more into SOAP related features first and then discuss how to do REST.

 

here [1]), that was used in the previous article, and see how we can change it to use SOAP 1.1. All that is required is a miniscule change, i.e. add a new option named useSOAP with the value 1.1, to the options array passed to the WSClient constructor.

$client = new WSClient(array("to" => "http://localhost/tutorial/payload/service.php", "useSOAP" => 1.1));

The rest of the code remains the same. When you run the sample with the above change, you will notice that the messages that you see with trace has the SOAP 1.1 namespace. Now, if you were paying attention, you may have noticed that, you did not need to change anything on the service script to use a different SOAP version. The idea is that with WSF/PHP, the service would use the correct SOAP version, based on the SOAP version used by the client. The number of possible values that could be given to the useSOAP option of the WSClient class is given in the API documentation.

 

TCPmon [2], you will notice that only the payload goes over the wire. The default HTTP method used when useSOAP is set to FALSE is POST. You can use another WSClient option, namely HTTPMethod, to use HTTP GET.

$client = new WSClient(array("to" => "http://localhost/tutorial/payload/service.php/getFactorial", "useSOAP" => FALSE,"HTTPMethod" => "GET"));

Note that, apart form adding the HTTPMethod option, we have also changed the to address of the service, from http://localhost/tutorial/payload/service.php to http://localhost/tutorial/payload/service.php/getFactorial. This is to include the operation name in the URL, which is required in case of a HTTP GET request. The following shows the messages sent and received over the wire with HTTP protocol elements, when you run the code with above changes.

Request:

GET /tutorial/payload/service.php/getFactorial?param=6 HTTP/1.1

User-Agent: Axis2/C

Host: 127.0.0.1:8080

Response: HTTP/1.1 200 OK

Date: Tue, 22 Jan 2008 05:45:49 GMT

Server: Apache/2.2.6 (Win32) PHP/5.2.5

X-Powered-By: PHP/5.2.5

Content-Length: 71

Content-Type: text/xml;charset=UTF-8

 

<getFactorialResponse> <result>720</result> </getFactorialResponse>

 

You can also send a request with your Web browser using the following URL to the PHP service: http://localhost/tutorial/payload/service.php/getFactorial?param=10

And the service would respond with the result 3628800, which is the factorial of 10. Also note that, like in the case of using SOAP 1.1, we did not touch the service script when using REST from client and the service still responded accordingly. This is because with WSF/PHP, service scripts are capable of either using SOAP or REST based on the request format. So you can write one service script and expose it both as SOAP as well as REST with a single deployment instance.

 

Download source code for the factorial sample [2].

 

WSO2 WSF/PHP Home Page [2]
  • WSO2 WSF/C Home Page [2]
  • WSO2 WSF/PHP Documentation Index [2]
  • Apache Axis2/C Home Page [3]
  •  

    Author

    Samisa Abeysinghe is a Software Architect at WSO2. He is a member of Apache Software Foundation and a project management committee member of the Apache Web services project. He is one of the pioneering members of the Apache Axis2/C project. samisa at wso2 dot com


    Source URL:
    http://wso2.org/library/3261