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,

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.