Milinda Pathirage’s Blog

Let the code talk for yourself!

Writing simple Web Service and Client using WSF/PHP

September 17th, 2008 · No Comments · Uncategorized

Implementing Echo Service

Let's write a simple web service using WSO2 WSF/PHP. This service will echo the incoming request to client. It will get the request payload from client and send it back to the client as the response. Example request and response:

Request:


<echo>
 Hello World
</echo>

Response


<echo>
 Hello World
</echo>

To write a Web Service using WSF/PHP you have to follow following steps:

  • Write PHP functions corresponding to the Web Service's operations.
  • Create a WSService object giving operation map and options.
  • Invoke the reply method of WSService object which will invoke the operation andprepare the response.

PHP function which implements the echo operation:


function echoOperation($inMsg){
    $outMsg = new WSMessage($inMsg->str);
    return $outMsg;
}

When implementing Web Services with WSF/PHP, we get the clients payload as a instance of WSMessage class in to the actual business logic. So we have to extract the string representation of the payload and create new WSMessage to return as response.

Creating the service object specifying the operations:


$service = new WSService(array("operations" => array("echoOperation")));

Previously implemented "echo" function will be called when a client send a message to invoke "echo" operation in the service.

Every service written using PHP to deploy in WSF/PHP must called the reply() function of WSService object. This call will invoke the correct PHP function corresponding to web service operation and prepare the reply to be send to the client.


$service->reply();

To deploy this web service, you just have to copy the php file into your web servers root direcotry. Let's say your file is echo_service.php, then you can go to http://localhost/echo_service.php and check whether services is working. If you have done everything write, the browser must show something similar to following:

samp

Implementing Echo Client

Sphere: Related Content

Tags:

0 responses so far ↓

  • There are no comments yet...Kick things off by filling out the form below.

Leave a Comment