【问题标题】:php nusoap return arrayphp nusoap 返回数组
【发布时间】:2011-10-03 08:55:11
【问题描述】:

我是网络服务的新手。

我想在 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
}

返回记录(数组)应该做哪些更改?

【问题讨论】:

    标签: php arrays return nusoap


    【解决方案1】:

    您的 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']

    【讨论】:

      【解决方案2】:

      而不是 return $items 写return array($items)

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

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

      并将返回更改为return array('return'=&gt;$items)

      【讨论】:

        猜你喜欢
        • 2016-03-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-26
        • 1970-01-01
        • 1970-01-01
        • 2011-02-07
        相关资源
        最近更新 更多