WSDL Generation with WSF/PHP - Arrays and Complex types

When you are working on code first approach you want to derive the contract WSDL from the code that you already have for the Web Service.

If you may have worked on Java or .NET Web Services you already familiarized with this approach. PHP developers are also provided the opportunity to work on this approach with WSF/PHP. You only need to give a simple introduction for your service operations and their parameter types as annotations, and you will get the WSDL generated in a flash.

Here is how you will put the annotations for a simple operation like add.


<?php

/**
* operation add
* @param int $x operand1 <- This is the PHP type
* maps to xs:int <- This is the schema type
* @param int $y operand2 <- This is the PHP type
* maps to xs:int <- This is the schema type
* @return int $result
*/
function add($x, $y)

{
   return array(“result” => $x + $y);

}

$operations = array(“add” => “add”);
$opParams = array(“add” => “MIXED”);

$svr = new WSService(array(“operations” => $operations,
                    “opParams” => $opParams,
                    “serviceName” => “Calculator”));

$svr->reply();

?>

Here just examine how the operation parameters are described. If this is not clear you can have a look at the wsdl generation manual[http://wso2.org/project/wsf/php/1.2.0/docs/wsdl_generation_api.html]. It describe the annotation format in a simple fashion.

Anyway in WSF/PHP 1.2.0 you didn’t have a way to specify when the input/output parameters are arrays. But in the upcomming 1.2.1 release you will get this facility.

For an example you can add an operation to add vectors like this,

/**
* operation addVectors
* @param array of int $x operand1 <- This is the PHP type
* maps to xs:int <- This is the schema type
* @param array of int $y operand2 <- This is the PHP type
* maps to xs:int <- This is the schema type
* @return array of int $result
* maps to xs:int <- This is the schema type
*/
function addVectors($x, $y)
{

   $result = array();
   for($i = 0; $i < count($x); $i ++)

   {
       $result[$i] = $x[$i] + $y[$i];
   }

   return array(“result” => $result);
}

NOTE: you should update the $operations and $opParams with the new operation.

Note the change in annotation notation, you have add a prefix called ‘array of’ (just ‘array’ is enough) and you will have the elements maxOccurs=”unbounded” in the WSDL means you are having arrays as input and output parameters in your service operation.

How about providing complex types as input and output parameters. That also will available in the WSF/PHP 1.2.1 release.

For an example say you want to add matrices. Here is how your code will look like.

/**
* operation addMatrices
* I have considered matrix as an array of Rows, which in tern array of columns
* @param array of object Row $x operand1 <- This is the PHP type
* maps to non xs:type <- This is required
* @param array of object Row $y operand2 <- This is the PHP type
* maps to non xs:type
* @return array of object Row $result
* maps to non xs:type
*/
function addMetrices($x, $y)
{

   $result = array();
   for($i = 0; $i < count($x); $i ++)

   {
       $row1 = $x[$i];
       $row2 = $y[$i];
       $result[$i] = new Row();
       $result[$i]->cols = array();
       for($j = 0; $j < count($row1->cols); $j ++)

       {
           $result[$i]-> cols[$j] = $row1[$j] + $row2[$j];
       }

   }
   return array(“result” => $result);
}

/**
* @namespace http://my.org/test/calculator/demo
*/
class Row
{
   /**
    * @property array of int cols columns
    * maps to xs:int
    */
   public $cols;
}
 

NOTE: you should update the $operations and $opParams with the new operation.

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

Leave a Reply

Spam protection by WP Captcha-Free