Friday, January 18, 2008

Tutorial2 - Hello World Using Web Service Framework for PHP with WSDL Generation

Yesterday I blogged a tutorial on how to write a Hello World Web Service Client and Service starting from a WSDL. So in order to follow that approach (Contract first, That is starting from a WSDL. I have described these terms in details here) you have to have a WSDL. Yes there are many tools which will help you generating WSDLs. But how if you can generate the WSDL from your PHP service itself.

In fact with WSF/PHP you can get it done easily. For the simplicity I will get the example service (the HelloWorld Service) I took yesterday.

First I write the service like following.

* Service.php

/* See how we put doc comment for the HelloWorld function */
* First whatever your introduction to the function
* @param string $name
* @return string $return

function HelloWorld($name)

/* Remember in the response we had 'return' element */
return array("return"=> "welcome ".$name."!");


/* Map of the service opertion "HelloWorld" to php functions "HelloWorld" */
$operations = array("HelloWorld" => "HelloWorld");

/* just tell your function parameters should be in mixed format,
that is here parameter will be the string with the name in it*/

$opParams = array("HelloWorld" => "MIXED");

/* Created the WSService */

$svr = new WSService(array(/* "wsdl" => "HelloWorld.wsdl", */ // we removed this line.
"bindingStyle"=>"doclit", // we added this line

"operations" => $operations,
"opParams" => $opParams));

/* Reply the client */



Just note the doc comment in the service operation "HelloWorld"..
* First whatever your introduction to the function
* @param string $name - Whatever the name mean
* @return string $return - Something to return

It explain the set of input parameters and the return value with its type. In fact this give a human reader an idea how should somebody should use this operation. But here in this particular case we have put it there just to give the WSF/PHP WSDL Generator a hint about this function (like what are the input and return types)

So put this in a Apache web root and go the following url in the localhost from a browser,


You will be able to retrieve the WSDL v 1.1 and WSDL v 2 for our service. Isn't that easy.

If you followed the yesterday my blog get the same client and just change the wsdl location to the new wsdl. (That is the one just typed in the browser)


* Client.php

/* create the WSClient with the given WSDL and my service endpoint
Note: Here im overwriting the endpoint declared in the WSDL */

$wsclient = new WSClient( array(
/* Here is my WSDL */
"wsdl" => "http://localhost/Service.php?wsdl2"));

/* we need to take the proxy object to call the wsdl operation */

$proxy = $wsclient->getProxy();

/* Right here I'm calling the HelloWorld function with argument "Hiro"
Remeber in the WSDL we had "HelloWorld" operation with name argument */

$ret_val = $proxy->HelloWorld(array("name" => "Hiro"));

/* Retrive the response. Just to recall, response had the element 'return' */
echo $ret_val["return"]."\n";


So you are done. You don't need any explanation of what are in the WSDL to do this tutorial. Since in this case you know what you should call since the service is designed by you and you know PHP.

But some one else (some even you don't know) who want to consume your service may not understand PHP (Say he is a really Java fan), and even though he is PHP guy you should not show your PHP service code to the unknown consumers.

That is where WSDL comes in to the play. You can give the WSDL (in fact just generated from the PHP) and let them consume the service. Surely there are many libraries and tools in Java (or any other language) which understand the WSDL and interpret it that language, so the consumer will find very easy to consume your service.

No comments: