One of the key feature of this release is improved WSDL mode and WSDL generation support.
In 1.2.0 we had support for simple WSDL generation like this.
<?php
/**
* addUser
* @param string $name here string is for php type
* this maps to xs:string (this is really optional declaration)
* @param int $age the age
* @return int $status
*/
function addUser($name, $age)
{
/* some logic add the user */
return 1;
}
/* map of service operation to php function */
$operations = array("addUser"=> "addUser");
/* this is required in the wsdl generation */
$opParams = array("addUser"=>"MIXED");
$service = new WSService(array("operations"=> $operations,
"opParams"=> $opParams,
"serviceName" => "User"));
$service->reply();
?>
There, the annotations are used to provide meta data about the Service, input and output parameter types.
If you are going to use the new release you would be able to improve the above code to more organized way like one in the following.
<?php
/**
* addUser
* @param object User $user : note the "object User" as the php type.
* @return int $status
*/
function addUser($user)
{
/* some logic add the user */
return 1;
}
/**
* @namespace http://im.me/types the namespace
*/
class User
{
/**
* @property string $name
* schema type xs:string - this is still optional
*/
public $name;
/**
* @property int $name
* xs:int
*/
public $age;
}
$clasmap = array("User"=> "User");
/* map of service operation to php function */
$operations = array("addUser"=> "addUser");
/* this is required in the wsdl generation */
$opParams = array("addUser"=>"MIXED");
$service = new WSService(array("operations"=> $operations,
"opParams"=> $opParams,
"classmap"=> $classmap,
/* the serviceName can provided as an option */
"serviceName" => "User"));
$service->reply();
?>
There we use class "User" to declare a schema type in the WSDL. So if you want to add method like getUsers you can share this type with the current addUser method. Here is how you would define getUsers method with correct annotations. Hopefully you should add this method to "operations" and "opParams" options as well.
/**This illustrate how you should extract the request and build the response in your logic when there are arrays and class types in your service operations.
* @param string $condition
* @return array of object User $users : note the array of is prefixed
* to the php type
*/
function getUsers($condition)
{
$user1 = new User();
$user1->name = "Hiro";
$user1->age =23;
$user2 = new User();
$user2->name = "Clair";
$user2->age = "14";
return array("users" => array($user1, $user2));
}
Well how you would write a client for this operations. Here is a sample client that use simple arrays to build requests and return response. Similarly if you set "classmap" option to the WSClient you would instead build the requests and extract responses from PHP class objects..
<?php
$client = new WSClient(array("wsdl"=>"http://localhost/mails/06_blog/MuchNewService.php?wsdl"));
$proxy = $client->getProxy();
//add user operation
$res = $proxy->addUser(array("user"=>
array("name" => "cyler", "age" => 28)));
print_r($res);
//getUsers operation
$res = $proxy->getUsers(array("conditions" => "none"));
print_r($res);
?>
Hope you all will enjoy this new set of features..
No comments:
Post a Comment