I’m not sure if it’s the best way of doing it, but this method of creating web services with wso2 works for me. A bonus here is that it doesn’t involve creating or modyfying any XML/WSDL files as all this is autogenerated or handled by the framework itself.
This example service will return N random digits from a given range.
wsdl autogeneration
wso2 has a nice feature of generating wsdl files based on service configuration. It can do even better than that – it can generate a wsdl file for your service based on the comments you use for functions that describe your service. Of course these comments have to be in a special format, but this format is nothing else than dotproject-style comments that can be used to automatically document your code. Here is an example, it doesn’t implement anything yet, it’s only used to generate a wsdl definition. To avoid URL changes in the service location specified in the generated wsdl I’m creating it in random/index.php in my web server’s root directory, which will be replaced by the service implementation later on:
<?php
/**
* @namespace http://server.com/random
*/
class RandomNumber
{
/**
* @var integer $number
*/
public $number;
}
/**
* @param integer $num number of random digits
* @param integer $min minimum random number
* @param integer $max maximum random number
* @return array of object RandomNumber $numbers array of random numbers generated
*/
function getRandom($num, $min, $max)
{
return new RandomNumber();
}
$service = new WSService(array("operations" => array("getRandom")));
$service->reply();
?>
When you access http://server.com/random/?wsdl you’ll get your wsdl file generated for the service documented above. Save it in your services directory as random.wsdl.
service and client autogeneration
Now we can use this file to generate both a service template and a sample client template.
In wso2-php package there is a file called wsdl2php.php. Use it to generate both templates:
> php wsdl2php.php -s random.wsdl > server.php
> php wsdl2php.php random.wsdl > client.php
you can keep a copy of your index.php file used to generate the wsdl file and replace it with the generated server.php
> cp index.php generate.php; mv server.php index.php
service implementation
At this point you can implement your service, edit your index.php file, read the comments there, and add this simple implementation to the getRandom function:
$response =
new getRandomResponse
();
for ($i =
1;
$i >=
$input->
num;
$i++
) {
$n =
new RandomNumber
();
$n->
number =
rand($input->
min,
$input->
max);
$response->
numbers[] =
$n;
}
return $response;
implementation of a sample client
and now modify your client to test it, under //TODO fill in the class fields of $input to match your business logic add:
$input->num = 10;
$input->min = 1;
$input->max = 100;
and implement the business logic to consume your response:
foreach ($response->
numbers as $n) {
printf("%d",
$n->
number);
}
Start client.php in your browser – you should be able to see 10 randomly generated numbers