php nusoap return array
I am new to web services.
I want to write a generic function in php nusoap server side which can query (fetch data from multiple tables) and return a dynamic array depending upon the results returned from mysql...
Here is the server code...
require_once ('../lib/nusoap.php');
$server = new soap_server;
$server->register('getallbook');
function getallbook()
{
$conn = mysql_connect('localhost','root','');
mysql_select_db('apexinventry', $conn);
$sql = "SELECT * FROM users";
$q = mysql_query($sql);
while($r = mysql_fetch_array($q)){
$items[] = array('cd'=>$r['id'],'title'=>$r['userid'],'author'=>$r['password'],'publisher'=>$r['groupid']);
}
return $items;
}
$server->service($HTTP_RAW_POST_DATA);
and here is client code......
require_once ('../lib/nusoap.php');
$client = new soapclient('http://127.0.0.1/test/server/index.php');
$response = $client->call('getallbook');
if($client->fault)
{
echo "FAULT: <p>Code: (".$client->faultcode.")</p>";
echo "String: ".$client->faultstring;
}
else
{
$r = $response;
$count = count($r);
?>
<table border="1">
<tr>
<th>C开发者_StackOverflowode</th>
<th>Title</th>
<th>Author</th>
<th>Publisher</th>
</tr>
<?php
for($i=0;$i<=$count-1;$i++){
?>
<tr>
<td><?php echo $r[$i]['cd'];?></td>
<td><?php echo $r[$i]['title'];?></td>
<td><?php echo $r[$i]['author'];?></td>
<td><?php echo $r[$i]['publisher'];?></td>
</tr>
<?php
}
?>
</table>
<?php
}
What changes should I do to return the records (array)?
How is the return value defined in your SOAP declaration? For example, here is something I have in my wsdl:
$server->wsdl->addComplexType('ResultObject',
'complexType',
'struct',
'all',
'',
array(
'result' => array('name' => 'result',
'type' => 'xsd:string'),
'addl_info' => array('name' => 'addl_info',
'type' => 'xsd:string')
)
);
Here is my function registration in the same wsdl:
$server->register('addGroupRequest', // method name
array('auth_name' => 'xsd:string',
'password' => 'xsd:string',
'group_objid' => 'xsd:int', // input parameters
'source_character_objid' => 'xsd:int', // input parameters
'message' => 'xsd:string'), // input parameters
array('ResultObject' => 'tns:ResultObject'), // output parameters
'urn:Groupwsdl', // namespace
'urn:Groupwsdl#addGroupRequest', // soapaction
'rpc', // style
'encoded', // use
'add group request for the character ' // documentation
);
And to get the array, I just call $return['addl_info']
or $return['result']
.
instead of return $items write return array($items)
If not working you can update $server->register('getallbook')
as
$server->register('getallbook', // method name
array('return' => 'xsd:string')
);
and change return to return array('return'=>$items)
精彩评论