I found this answer from internet and it helps me a lot:
Imagine the request is going to look like this:
XML Diagram 1:
<CountBasketItems>
<Basket>
<fruits>
<fruit>
<name>Apple</name>
<color>red</color>
</fruit>
<fruit>
<name>Bananna</name>
<color>yellow</color>
</fruit>
<fruit>
<name>Apple</name>
<color>green</color>
</fruit>
</fruits>
<vegetables>
<vegetable>
<name>Orion</name>
<color>yellow</color>
</vegetable>
<vegetable>
<name>Carot</name>
<color>orange</color>
</vegetable>
<vegetable>
<name>Orion</name>
<color>red</color>
</vegetable>
</vegetables>
</Basket>
</CountBasketItems>
The real question is: how do I setup my parameter array to accommodate this. The answer:
$fruits = array();
$fruits[] = array(‘name’ => ‘Apple’, ‘color’ => ‘red’);
$fruits[] = array(‘name’ => ‘Bananna’, ‘color’ => ‘yellow’);
$fruits[] = array(‘name’ => ‘Apple’, ‘color’ => ‘green’);
$vegetables = array();
$vegetables[] = array(‘name’ => ‘Orion’, ‘color’ => ‘yellow’);
$vegetables[] = array(‘name’ => ‘Carrot’, ‘color’ => ‘orange’);
$vegetables[] = array(‘name’ => ‘Orion’, ‘color’ => ‘red’);
$basket = array ( ‘fruits’ => $fruits, ‘vegetables’ => $vegetables );
Here is what $basket looks like when I run print_r($basket):
Array
(
[fruits] => Array
(
[0] => Array
(
[name] => Apple
[color] => red
)
[1] => Array
(
[name] => Bananna
[color] => yellow
)
[2] => Array
(
[name] => Apple
[color] => green
)
)
[vegetables] => Array
(
[0] => Array
(
[name] => Orion
[color] => yellow
)
[1] => Array
(
[name] => Carrot
[color] => orange
)
[2] => Array
(
[name] => Orion
[color] => red
)
)
)
Here is a complete example, server and client:
server.php:
configureWSDL('maxfruitwsdl', 'urn:maxfruitwsdl');
// Put the WSDL schema types in the namespace with the tns prefix
$server->wsdl->schemaTargetNamespace = 'urn:maxfruitwsdl';
// Register the data structures used by the service
$server->wsdl->addComplexType(
'Fruit',
'complexType',
'struct',
'all',
'',
array(
'name' => array('name' => 'name', 'type' => 'xsd:string'),
'color' => array('name' => 'color', 'type' => 'xsd:string'),
)
);
$server->wsdl->addComplexType(
'Fruits',
'complexType',
'array',
'',
'SOAP-ENC:Array',
array(),
array(
array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:Fruit[]')
),
'tns:Fruit'
);
$server->wsdl->addComplexType(
'Vegetable',
'complexType',
'struct',
'all',
'',
array(
'name' => array('name' => 'name', 'type' => 'xsd:string'),
'color' => array('name' => 'color', 'type' => 'xsd:string'),
)
);
$server->wsdl->addComplexType(
'Vegetables',
'complexType',
'array',
'',
'SOAP-ENC:Array',
array(),
array(
array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:Vegetable[]')
),
'tns:Vegetable'
);
$server->wsdl->addComplexType(
'Basket',
'complexType',
'struct',
'all',
'',
array(
'fruits' => array('name' => 'fruits', 'type' => 'tns:Fruits'),
'vegetables' => array('name' => 'vegetables', 'type' => 'tns:Vegetables'),
)
);
// Register the method to expose
$server->register('CountBasketItems', // method name
array('Basket' => 'tns:Basket'), // input parameters
array('return' => 'xsd:int'), // output parameters
'urn:maxfruitwsdl', // namespace
'urn:maxfruitwsdl#CountBasketItems', // soapaction
'rpc', // style
'encoded', // use
'Return the number of items in the basket' // documentation
);
function CountBasketItems( $basket )
{
return count($basket['fruits']) + count($basket['vegetables']);
}
// This returns the result
$server->service($HTTP_RAW_POST_DATA);
?>
client.php
$fruits[] = array('name' => 'Bananna', 'color' => 'yellow');
$fruits[] = array('name' => 'Apple', 'color' => 'green');
$vegetables = array();
$vegetables[] = array('name' => 'Orion', 'color' => 'yellow');
$vegetables[] = array('name' => 'Carrot', 'color' => 'orange');
$vegetables[] = array('name' => 'Orion', 'color' => 'red');
$basket = array ( 'fruits' => $fruits, 'vegetables' => $vegetables );
print "My Basket:";
echo str_replace(' ', ' ', nl2br(htmlspecialchars(print_r($basket, true))));
// Create the client instance
$client = new soapclient('http://www.modphp.org/nusoap/server.php?wsdl', true);
// Check for an error
$err = $client->getError();
if ($err) {
// Display the error
echo 'Constructor error' . $err . '';
// At this point, you know the call that follows will fail
}
// Create the proxy
$proxy = $client->getProxy();
// Call the SOAP method
$result = $proxy->CountBasketItems($basket);
// Check for a fault
if ($proxy->fault) {
echo 'Faul';
print_r($result);
echo '';
} else {
// Check for errors
$err = $proxy->getError();
if ($err) {
// Display the error
echo '
Error' . $err . '';
} else {
// Display the result
echo 'Result of CountBasketItems';
print_r($result);
echo '';
}
}
// Display the request and response
echo 'Request';
echo str_replace(' ', ' ', nl2br(htmlspecialchars($proxy->request, ENT_QUOTES)));
echo 'Response';
echo str_replace(' ', ' ', nl2br(htmlspecialchars($proxy->response, ENT_QUOTES)));
// Display the debug messages