Simple Calculator - Demo of use of WSDL mode

Tuesday, March 11, 2008

Here are the steps I followed to create a simple Calculator Service + client.

1. Stated with the CalculatorDoc.wsdl found in here, http://svn.apache.org/repos/asf/webservices/axis2/trunk/c/test/resources/wsdl/CalculatorDoc.wsdl

2. Generated server side code using the wsdl2php.php script packed with the WSO2 WSF/PHP 1.2.1.

3. Run the wsdl2php.php script from command prompt with the following arguments.

/my/home/scripts/wsdl2php.php -s CalculatorDoc.wsdl

Here '-s' means for the server side.

4. I redirected output to the service.php in my linux system, so the command was like
/my/home/scripts/wsdl2php.php -s CalculatorDoc.wsdl > service.php
In a case this this doesn't work for you copy and paste the code to the service.php file.

5. Filled the adding/ subtraction/ multiplication/ division algorithm inside method bodies. There are helping comments which guide you what are the types of parameters and return value in the function, here is my way of doing it.

// define PHP functions that maps to WSDL operations 
function add($input) {
// TODO: fill in the business logic
// NOTE: $input is of type add


$a = $input->arg_0_0;
$b = $input->arg_1_0;


$c = $a + $b;
// NOTE: should return an object of type addResponse

$res = new addResponse();
$res->addReturn = $c;


return $res;
}


function sub($input) {

// TODO: fill in the business logic
// NOTE: $input is of type sub

$a = $input->arg_0_1;
$b = $input->arg_1_1;


$c = $a - $b;
// NOTE: should return an object of type subResponse

$res = new subResponse();
$res->subReturn = $c;


return $res;
}


function mul($input) {

// TODO: fill in the business logic
// NOTE: $input is of type mul

$a = $input->arg_0_2;
$b = $input->arg_1_2;


$c = $a * $b;
// NOTE: should return an object of type mulResponse

$res = new mulResponse();
$res->mulReturn = $c;


return $res;
}


function div($input) {

// TODO: fill in the business logic
// NOTE: $input is of type div

$a = $input->arg_0_3;
$b = $input->arg_1_3;


$c = $a / $b;
// NOTE: should return an object of type divResponse

$res = new divResponse();
$res->divReturn = $c;


return $res;
}


6. Now put the service.php in to your web root directory or some sub directory inside the web root and make sure it is correctly deployed by typing the location in the browser. For an example, I put my service.php inside calculator sub directory inside the web root, so I tested in the browser by typing http://localhost/calculator/service.php

Note: that this doesn't test the algorithm of the service, but just it make sulre the service and the operation set are correctly deployed.

7. So now you can write a client to test the service operation, You can generate a client with the following command,

/my/home/scripts/wsdl2php.php CalculatorDoc.wsdl

Note that the '-s' is dropped.

8. Go through the TODO comments and fill the client logic by filling sample input to test the service,

   // create input object and set values
$input = new add();
//TODO: fill in the class fields of $input to match your business logic


$input->arg_0_0 = 3;
$input->arg_1_0 = 4;

// call the operation

$response = $proxy->add($input);
//TODO: Implement business logic to consume $response, which is of type addResponse

echo "3 + 4 = {$response->addReturn}\n";



$input = new sub();
//TODO: fill in the class fields of $input to match your business logic

$input->arg_0_1 = 3;
$input->arg_1_1 = 4;


// call the operation
$response = $proxy->sub($input);
//TODO: Implement business logic to consume $response, which is of type subResponsee


echo "3 - 4 = {$response->subReturn}\n";

$input = new mul();
//TODO: fill in the class fields of $input to match your business logic


$input->arg_0_2 = 3;
$input->arg_1_2 = 4;


// call the operation
$response = $proxy->mul($input);
//TODO: Implement business logic to consume $response, which is of type mulResponsee


echo "3 * 4 = {$response->mulReturn}\n";

$input = new div();
//TODO: fill in the class fields of $input to match your business logic


$input->arg_0_3 = 3;
$input->arg_1_3 = 4;


// call the operation
$response = $proxy->div($input);
//TODO: Implement business logic to consume $response, which is of type divResponsee


echo "3 / 4 = {$response->divReturn}\n"


9. One more thing, The wsdl is coded with it s endpoint, but for you to test you need to change the endpoint to where you have actually test the service, for an example in my case to " http://localhost/calculator/service.php"
You can do the change in the wsdl. But there is an option in the WSClient constructor where you can give the service endpoint.
So my new WSClient constructor looks like this,
   // create client in WSDL mode
$client = new WSClient(array ("wsdl" =>"CalculatorDoc.wsdl",
"to" => "http://localhost/calculator/service.php",
"classmap" => $class_map));



10. That is all I did, And Finally run the client with the following command,
php client.php

And I received the expected output as following,
3 + 4 = 7
3 - 4 = -1
3 * 4 = 12
3 / 4 = 0.75

No comments: