php nusoap返回数组

时间:2011-07-12 06:32:26

标签: php arrays return nusoap

我是网络服务新手。

我想在php nusoap服务器端编写一个泛型函数,它可以查询(从多个表中获取数据)并根据mysql返回的结果返回一个动态数组...

这是服务器代码......

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);

这是客户代码......

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>Code</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
}

我应该做些什么更改才能返回记录(数组)?

2 个答案:

答案 0 :(得分:0)

SOAP声明中的返回值是如何定义的?例如,这是我在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')
      )
);

这是我在同一个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
);

要获得数组,我只需拨打$return['addl_info']$return['result']

答案 1 :(得分:0)

代替返回$ items写入return array($items)

如果不起作用,您可以将$server->register('getallbook')更新为

$server->register('getallbook',                // method name
    array('return' => 'xsd:string')
);

并将更改返回到return array('return'=>$items)