wsdl return an array of complex types
I have defined a web service that will return the data from my mysql data base.
I have written the web service in php.
Now I have defined a complex type as follows:
$server->wsdl->addComplexType(
'Category',
'complexType',
'struct',
'all',
'',
array(
'category_parent_id' => array('name' => 'category_parent_id', 'type' => 'xsd:int'),
'category_child_id' => array('name' => 'category_child_id', 'type' => 'xsd:int'),
'category_list' => array('name' => 'category_list', 'type' => 'xsd:int')
)
);
The above complex type is a row in a table in my database.
Now my function must send an array of these rows so how do I achieve the same
My code is as follows:
require_once('./nusoap/nusoap.php');
$server = new soap_server;
$server->configureWSDL('productwsdl', 'urn:productwsdl');
// Register the data structures used by the service
$server->wsdl->addComplexType(
'Category',
'complexType',
'struct',
'all',
'',
array(
'category_parent_id' => array('name' => 'category_parent_id', 'type' => 'xsd:int'),
'category_child_id' => array('name' => 'category_child_id', 'type' => 'xsd:int'),
'category_list' => array('name' => 'category_list', 'type' => 'xsd:int')
)
);
$server->register('getaproduct'开发者_Python百科, // method name
array(), // input parameters
//array('return' => array('result' => 'tns:Category')), // output parameters
array('return' => 'tns:Category'), // output parameters
'urn:productwsdl', // namespace
'urn:productwsdl#getaproduct', // soapaction
'rpc', // style
'encoded', // use
'Get the product categories' // documentation
);
function getaproduct()
{
$conn = mysql_connect('localhost','root','');
mysql_select_db('sssl', $conn);
$sql = "SELECT * FROM jos_vm_category_xref";
$q = mysql_query($sql);
while($r = mysql_fetch_array($q))
{
$items[] = array('category_parent_id'=>$r['category_parent_id'],
'category_child_id'=>$r['category_child_id'],
'category_list'=>$r['category_list']);
}
return $items;
}
// Use the request to (try to) invoke the service
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
I figured the answer myself after searching the internet.
Following is the code to create a complex data type. Here I am creating a datatype Person whch has firstname, age and gender as its data members.
$server->wsdl->addComplexType(
'Person',
'complexType',
'struct',
'all',
'',
array(
'firstname' => array('name' => 'firstname', 'type' => 'xsd:string'),
'age' => array('name' => 'age', 'type' => 'xsd:int'),
'gender' => array('name' => 'gender', 'type' => 'xsd:string')
)
);
Next we must create another new datatype which is an array of the datatype we have created. I call it the person array and the code for it is below:
$server->wsdl->addComplexType(
'PersonArray', // Name
'complexType', // Type Class
'array', // PHP Type
'', // Compositor
'SOAP-ENC:Array', // Restricted Base
array(),
array(
array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:Person[]')
),
'tns:Person'
);
Now I registered a function called getPeople which takes no input parameters but return an array of persons as:
$server->register(
'getPeople', // method name
array(), // input parameters
array('return' => 'tns:PersonArray'), // output parameters
'urn:hellowsdl2', // namespace
'urn:hellowsdl2#getPeople', // soapaction
'rpc', // style
'encoded', // use
'Return an array of people' // documentation
);
and programmed the function to return some dummy data as:
function getPeople()
{
$peopleArray = array();
$peopleArray[] = array(
'firstname' => "Anand",
'age' => 25,
'gender' => "Male"
);
$peopleArray[] = array(
'firstname' => "Sandhya",
'age' => 21,
'gender' => "Female"
);
return $peopleArray;
}
by the way I am sorry I haven't mentioned but all this code is in PHP.
Hope this helps someone.
精彩评论