Wednesday, January 30, 2008

Implementing a Service using a PHP Class

WSF/PHP supports implementing a service using a php class. One of the advantages of implementing the service Operations with in a class is that is gives the ability to pass arguments to the constructor of the class.

Following is a simple example.

class Bar{

private $value = "";
function __construct($str){
$this->value = $str;
}

function echoValue($inMessage){
$responsePayloadString = <<<XML
<ns1:echostring ns1="<a href=">http://wso2.org/projects/wsf/php</a>">
<value>$this->value</value> </ns1:echoString>
XML;
return new WSMessage($responsePayloadString);
}

}

$operations = array("echoString" => "echoValue");

$service = new WSService(
array( "classes" => array("Bar" =>
array("operations" =>$operations,
"args" => array("Hello")
)
)));


$service->reply();

Note that Bar class is just a very simple PHP class. It's constructor takes a string argument and echo that value.

Next you have to define the Operations array with is very simple.
Next you need to tell the WSService class about your class, its defined operations and its arguements.

WSService options array accepts a parameter named "classes" which is an associative array. Its key is the class name and value is another array with contains the "operations" options and "args" options. "operations" are the operations defined in the class. The "args" is an array of arguments to the constructor of the corresponding class. Since the "Bar" class has a single argument constuctor, we are providing an string.

1 comment:

  1. Hello Nandika,

    I'm beginner with the WSO2/WSF and just tried to use the present tutorial to implement a webservice.


    When I try the webservice with soapUI, i get this message :

    HTTP/1.0 500 Internal Server Error
    Date: Wed, 16 May 2012 13:48:08 GMT
    Server: Apache/2.2.20 (Ubuntu)
    X-Powered-By: PHP/5.3.6-13ubuntu3.7
    Vary: Accept-Encoding
    Content-Encoding: gzip
    Content-Length: 20
    Connection: close
    Content-Type: text/html

    Any idea ?

    Thanks
    Thierry

    ReplyDelete