Make your PHP Webservice available for both REST and SOAP consumers

Tuesday, January 8, 2008

You may have noticed many public web services have deployed their services both as REST services and SOAP services, (e.g. Flickr). You may like to have this feature in your services as well. If you are working with WSO2 Web Service Framework for PHP you don't need to worry about this. Because its engine itself take care of publishing your service both in REST and SOAP form.

In fact one of the indirect advantage, I experienced from this feature is, it allows us to test our Webservice logic in much easier path.

For an example think you are a teacher in a high school and you want to publish examination results through a Webservice.

1 <?php
2 /* exam.php: Simple PHP Webservice for publish students marks */
4 /* getResults: This is the operation you going to expose as the Webservice operaion */
5 function getResults($message) {
7 /* you hard code the results in an array */
9 $results_map = array("Hiro" => "A+",
10 "Clair" => "A+",
11 "Peter" => "A+",
12 "Mohinder" => "A+",
13 "Ando" => "A",
14 "Niki" => "B",
15 "Sylar" => "F",
16 /* you are not worrying to put others results, since they all have 'F's */);
18 /* extract the firstName using SimpleXML techniques */
20 $xml = new SimpleXMLElement($message->str);
21 $firstName = trim($xml->firstName);
24 /* you look for the result for the firstName */
25 $result = $results_map[firstName];
26 if($result === NULL)
27 {
28 /* sorry I dont have time to give you a good result.. :( */
29 $result = "F";
30 }
32 /* build the response xml and return */
33 return "<result>$result</result>";
35 }
37 /* create a WSService and specify the service operation */
38 $service = new WSService(array("operations" => array("getResults")));
40 $service->reply();
42 ?>

So you are done writing and deploying the service by copying it to the Apache Root directory. But you may feel little lazy to write a simple client to test this service. Although it is very simple to write a client (either SOAP or REST) with WSF/PHP, much easier way is to simply open a browser and type following text in the address bar.

" (That is add the the "exam.php/getResults?firstName=Hiro" to the URL where you have hosted your service)

If you see something like


you are done. You can continue testing your service by typing several names and observing whether it returns the correct one.

Actually what you have done here is sending a 'REST' request to the service. If this is working fine your logic is correct. And it will work for the 'SOAP' request as well.

So availability of the services both in REST and SOAP form is a cool feature in WSO2 Webservice framework for PHP which is really helpful not only for consumers but also for service developers.


Lars Strojny said...

The issue is, that your example may be an REST example, but it is not RESTful. The idea of REST was to represent resources as URLs and utilize the HTTP methods for actions. Your example is something like using RPC over HTTP, but that's what XML/RPC is designed for. So no need to reinvent. A good RESTful example would be the following: would return your payload. As you can see, the resource is a unique URL.
A more complicated example: the teacher adds a new exam result:
Request excerpt:

Response excerpt:

You spot the difference?

Dimuthu said...

Yea I think I got what you spot. Thanks very much for point out it here..

Meanwhile I think I was clear on the point that how you can deploy the service in both REST (Well, I agree it is not RESTful) and SOAP form without doing anything special.

I think why we are not focusing much on RESTful approach is simply because our main target is to provide more WS-* Support. Providing REST support is an additional feature.

Sami said...

Value of WSF/PHP is mainly in the areas like security. With REST, you cannot go beyond the transport security (https). In other words, there are no mechanisms to secure the message itself. That is where SOAP based security comes into rescue, where you can secure the message, beyond the transport. That is the main focus of WSF/PHP, to support WS-*.
Of course there are use cases for REST, where the added overhead of SOAP is not required. Hence the level of REST support with WSF/PHP.
The current level of REST support of WSF/PHP can be improved, by improving the Apache Axis2/C engine that it is based on.

Watson said...

It’s too informative post. Your blog have many posts which are really too Good and very useful.
Web Design Company