PHP NuSoap Server返回复杂类型的数组

时间:2014-03-16 21:20:12

标签: php soap nusoap

有许多与此相关的问题尚未得到解答,我正在寻找解决方案。 所以我有一个带有NuSoap的Soap服务器,我正在尝试返回一个数据库行数组(这里模拟为PS4游戏实体的硬编码列表)。

下面的代码适用于(稍作修改)返回单个数组,但是我无法返回超过1个数组。

有没有人有这方面的解决方案,因为我花了一天时间在线尝试各种解决方案以及下面的代码:

//Settings

$ns = 'http://'.$_SERVER['HTTP_HOST'].'/index.php/soapserver/';
$this->load->library("Nusoap_library");
$this->nusoap_server = new soap_server();
$this->nusoap_server->configureWSDL("Nusoap Server", $ns);
$this->nusoap_server->wsdl->schemaTargetNamespace = $ns;

//Complex Types and Function registers on server

// Complex Type "Game":
$this->nusoap_server->wsdl->addComplexType(
    'Game', // the type's name
    'complexType',
    'struct',
    'all',
    '',
    array(
        'title' => array('name'=>'title','type'=>'xsd:string'),
        'description' => array('name'=>'description','type'=>'xsd:string'),
        'price' => array('name'=>'price','type'=>'xsd:string'),
    )
);


// Complex Type "Games" (array of Game):
$this->nusoap_server->wsdl->addComplexType(
    'Games',
    'complexType',
'array',
'',
'SOAP-ENC:Array',
array(),
array(
    array(
        'ref'=>'SOAP-ENC:arrayType',
        'wsdl:arrayType'=>'tns:Game[]'
    )
),
'tns:Game'
);

// Get_Games
$this->nusoap_server->register(
    'get_games',
    array('instance_id' => 'xsd:string'),
    array('return'=>'tns:Games'),
    '',
    false,
    'rpc',
    'encoded',
    'Return array of Games'
);

// [REGISTERED METHODS]:
function get_games($instance_id)
{
    // [Hardcoded Games list]:
    $games = array(
        array(
            'title' => 'Killzone: Shadow Fall',
            'description' => 'test desc ',
            'price' => '49.99'
        ),
        array(
            'title' => 'Battlefield',
            'description' => 'blah blah',
            'price' => '54.99'
        )
    );

    return $games;
}

问题:

我需要对上述代码进行哪些更改才能让此服务器返回游戏实体数组。

如果我返回$ games [0]并对代码进行一些细微更改,它可以正常工作,但我似乎无法使上述代码生效。

任何帮助或建议表示赞赏......

编辑:

我一直得到以下错误:

    Error

    XML error parsing SOAP payload on line 10: Invalid document end

    Response

    HTTP/1.1 200 OK
    Date: Sun, 16 Mar 2014 20:35:35 GMT
    Server: Apache/2.4.7 (Win32) OpenSSL/1.0.1e PHP/5.5.6
    X-Powered-By: PHP/5.5.6
    X-SOAP-Server: NuSOAP/0.9.5 (1.123)
    Content-Length: 1688
    Content-Type: text/xml; charset=ISO-8859-1

     <div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

    <h4>A PHP Error was encountered</h4>

    <p>Severity: Notice</p>
    <p>Message:  Array to string conversion</p>
    <p>Filename: lib/nusoap.php</p>
    <p>Line Number: 6132</p>

    </div><div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

    <h4>A PHP Error was encountered</h4>

    <p>Severity: Notice</p>
    <p>Message:  Array to string conversion</p>
    <p>Filename: lib/nusoap.php</p>
    <p>Line Number: 6132</p>

    </div><?xml version="1.0" encoding="ISO-8859-1"?><SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://localhost/index.php/soapserver/"><SOAP-ENV:Body><ns1:get_gamesResponse xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/"><return xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="tns:Game[2]"><item xsi:type="tns:Game"><title xsi:type="xsd:string">Killzone: Shadow Fall</title><description xsi:type="xsd:string">test desc</description><price xsi:type="xsd:string">49.99</price></item><item xsi:type="tns:Game"><title xsi:type="xsd:string">Battlefield</title><description xsi:type="xsd:string">blah blah</description><price xsi:type="xsd:string">54.99</price></item></return></ns1:get_gamesResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>

1 个答案:

答案 0 :(得分:2)

这就是我所做的:

// Complex Array Keys and Types ++++++++++++++++++++++++++++++++++++++++++
$server->wsdl->addComplexType('notaryConnectionData','complexType','struct','all','',
        array(
                'id' => array('name'=>'id','type'=>'xsd:int'),
                'name' => array('name'=>'name','type'=>'xsd:string')
        )
);
// *************************************************************************

// Complex Array ++++++++++++++++++++++++++++++++++++++++++
$server->wsdl->addComplexType('notaryConnectionArray','complexType','array','','SOAP-ENC:Array',
        array(),
        array(
            array(
                'ref' => 'SOAP-ENC:arrayType',
                'wsdl:arrayType' => 'tns:notaryConnectionData[]'
            )
        )
);
// *************************************************************************

// This is where I register my method and use the notaryConnectionArray
$server->register("listNotaryConnections",
                array('token' => 'xsd:string'),
                array('result' => 'xsd:bool', 'notary_array' => 'tns:notaryConnectionArray', 'error' => 'xsd:string'),
                'urn:closingorder',
                'urn:closingorder#listNotaryConnections',
                'rpc',
                'encoded',
                'Use this service to list notaries connected to the signed-in title company.');

// In my function, I query the data and do:
$list = array();
$results = mysql_query($query);
while($row = mysql_fetch_assoc($results)) {
    array_push($list, array('id' => intval($row['na_id']), 'name' => $row['agency_name']));
}

return array("result" => true, "notary_array" => $list);

// The output is:
Array
(
    [result] => 1
    [notary_array] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [name] => Agency 1
                )

            [1] => Array
                (
                    [id] => 3
                    [name] => Agency 3
                )

            [2] => Array
                (
                    [id] => 4
                    [name] => Agency 4
                )

        )

    [error] => 
)

希望这有帮助。