Writing a Simple REST and SOAP Service With PHP

WSF/PHP enables you to write both REST and SOAP services in PHP from a single script. I have written about how you can expose your Database as a REST and SOAP services in few of my previous posts using the Data Service capability of WSF/PHP. But there can be situations where your service is not based on a Database. For an example it can use results of some calculations, or a mashup calling other services. In that case you will prefer to write the service logic yourself. Here is how you can do it.

Lets think we have weather forecast data (may be from another service) and I want to make a web service using it and make it accessible via both REST and SOAP protocols.

In our demo service we give forecasts of temperature, humidity and some other parameters for a given date. So I expect

SOAP request payload as following.

<weatherReport>
  <date>{date}</date>
  <parameter>{parameter}</parameter>
</weatherReport>

And REST Request will be like

weatherReport/{date}/forecast/{parameter}

Note that here parameter can hold values like temperature, humidity or sunset-time.

First we declare our operation and the REST Request Mapping like this,

$operations = array("weatherReport" => "weather_report");
$restmap = array("weatherReport" =>
				array("HTTPMethod" =>"GET",
				      "RESTLocation" => "weatherReport/{date}/forecast/{parameter}"));

When you declare your rest mapping like above , in the service operation you will have the same request XML for both SOAP and REST form like this,

<weatherReport>
  <date>{date}</date>
  <parameter>{parameter}</parameter>
</weatherReport>

So in your service logic you just handling the request in only above format. You can easily extract out the request parameters using SimpleXML functions and return the corresponding result. So you service operation would be something like this,

function weather_report($in_message) {

	// create the simple xml element for the request xml
	$request_xml = new SimpleXMLElement($in_message->str);

	// extract out the parameter and the date
	$date = $request_xml->date;
	$parameter = $request_xml->parameter;

	// It is up to you to retrun the weather data ($result) for the requested date and parameter

	return "<response>$result</response>";
}

Finally you create the WSService object with the “operations” and “RESTMapping” and call its reply method which actually response to the requests.

$service = new WSService(array("operations" => $operations, "RESTMapping" => $restmap));
$service->reply();

You just created a web service which will handle both SOAP and REST requests.

This entry was posted in REST, RESTful, Tutorial/Guide, web services, wsf/php, wso2 and tagged , , , , , . Bookmark the permalink.

6 Responses to Writing a Simple REST and SOAP Service With PHP

  1. Pingback: DEMO on a SOAP and REST Client with PHP | Dimuthu's Blog

  2. Napoleon says:

    Wonderful info! Keep up this great work!

  3. Pedro says:

    Where is the $result variable being defined?

  4. dimuthu says:

    Hi,
    In fact I’ve ignored how $result come. There I assume user have some function that can get $result from the date and parameters like below,
    $result = whether_data_generator($date, $parameter)

    In fact I use this just to show how you can write a single service to server both REST and SOAP request. If you want to know how to consume a real word whether service, have look at this screencast, http://wso2.org/library/presentations/wso2-wsf-php-screencast-consume-service-using-wsf-php.

  5. alex says:

    Thanks, excellent article.

  6. Roshith Kaniyamchalil says:

    Great Article. Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *