WSF/PHP allows you to develop web services with both Contract First (Starting from WSDL) and Code First Approaches. From these two, Contract First approach is the most famous and most recomended way of developing a Webservice. There you need to have a WSDL to start with. I will take the http://labs.wso2.org/wsf/php/example.xml as our WSDL.
Generate The Service Code
When you download the WSF/PHP pack there is a script called ‘wsdl2php.php’ in the script directory. Open a command line and run it with the following options. Or you can use the online wsdl2php tool
php wsdl2php.php http://labs.wso2.org/wsf/php/example.xml -s
Complete The Business Logic
Complete the TODO section of the generated file. Apparently what is left to do is filling the business logic for each function corresponding to the service operations in your WSDL. You are given the hint to the input and output parameters. Here is how I fill the logic for the simpleAdd function.
function simpleAdd($input) { // TODO: fill in the business logic // NOTE: $input is of type simpleAdd // NOTE: should return an object of type simpleAddResponse $res = new simpleAddResponse(); $res->return = $input->param0 + $input->param1; return $res; }
Advanced Types? – Read Generated Hints for Class variables
So how about filling the matrix add logic. Can be it be really complex?. No, what you need to observe is there the class member variables contains not only simple types but also another Class type. There is a comment on each member variables to hint its type. So the logic for the matrix addition is simple as this,
function matrixAdd($input) { // TODO: fill in the business logic // NOTE: $input is of type matrixAdd // NOTE: should return an object of type matrixAddResponse $matrix0 = $input->param0; $matrix1 = $input->param1; $matrix2 = new Matrix(); $matrix2->rows = array(); if(count($matrix0->rows) == count($matrix1->rows)) { // considering only happy path for($i = 0; $i < count($matrix0->rows); $i ++ ) { $row0 = $matrix0->rows[$i]; $row1 = $matrix1->rows[$i]; $row2 = new MatrixRow(); $matrix2->rows[] = $row2; $row2->columns = array(); if(count($row0->columns) == count($row1->columns)) { for($j = 0; $j< count($row0->columns); $j ++ ) { $col0 = $row0->columns[$j]; $col1 = $row1->columns[$j]; $col2 = $col0 + $col1; $row2->columns[] = $col2; } } } } $res = new matrixAddResponse(); $res->return = $matrix2; return $res; }
Pingback: WSDL2PHP 2 Minutes Introduction | Dimuthu's Blog