在nusoap中返回数组的数组

时间:2018-07-25 08:58:32

标签: php web-services soap nusoap soapserver

我正在使用NUSOAP用PHP开发Web服务,并且找到了返回数组的方法。 但是,我正在尝试做他返回数组与数组内。 我尝试返回的数组示例:

$return = array(
    'lastName' => "Dupond",
    'firstName' => array("Bryan", "Michael"),
    'nationality' => "NA",
    'gender' => "M" 
);

实际上,当我返回此数组时,出现“无法序列化结果”错误。 我肯定知道这是因为它不了解firstName中的数组。

我的代码实际上是什么样的:

function hello($name, $firstname)
{
    $lastName = "Name";
    $firstName = array("firstName1","firstName2");
    $nationality = "NA";
    $gender = "M";

    $return= array(
        'lastName' => $lastName,
        'firstName' => $firstName,
        'nationality' => $nationality,
        'gender' => $gender 
    );

    return $return;
}

$server = new soap_server();
$server->configureWSDL("CallHello", "urn:CallHello");
$server->wsdl->addComplexType(
    'Game', // the type's name
    'complexType',
    'struct',
    'all',
    '',
    array(
        'lastName' => array('name'=>'test','type'=>'xsd:string'),
        'firstName' => array('name'=>'test','type'=>'tns:test'),
        'nationality' => array('name'=>'test','type'=>'xsd:string'),
        'gender' => array('name'=>'test','type'=>'xsd:string'),
    )
);

$server->wsdl->addComplexType(
   'test', // the type's name
   'complexType',
   'struct',
   'all',
    '',
    array(
            array('name'=>'firtsname1','type'=>'xsd:string'),
            array('name'=>'firstname2','type'=>'xsd:string')
    )
);

$server->wsdl->addComplexType(
    'Games',
    'complexType',
    'array',
    '',
    'SOAP-ENC:Array',
    array(),
    array(
        array(
               'ref'=>'SOAP-ENC:arrayType',
               'wsdl:arrayType'=>'tns:Game[]'
        )   
    ),
    'tns:Game'
);

$server->register("hello",
    array("name" => "xsd:string","firstname" => "xsd:xml"),
    array("return" => "tns:Game"),
    "urn:CallHello",
    "urn:CallHello#hello",
    "rpc",
    "encoded",
    "Get a Person Data");

//$server->service('php://input');
$server->service($HTTP_RAW_POST_DATA);

这是我的wsdl的相关部分:

<xsd:complexType name="Game">
    <xsd:all>
        <xsd:element name="lastName" type="xsd:string"/>
        <xsd:element name="firstName" type="tns:test"/>
        <xsd:element name="nationality" type="xsd:string"/>
        <xsd:element name="gender" type="xsd:string"/>
    </xsd:all>
</xsd:complexType>

我想要的也是如此:

<xsd:complexType name="Game">
    <xsd:all>
        <xsd:element name="lastName" type="xsd:string"/>
        <xsd:complexType name="firstName">
            <xsd:all>
                <xsd:element name="firstName1" type="xsd:string"/>
                <xsd:element name="firstName2" type="xsd:string"/>
            </xsd:all>
        </xsd:complexType>
        <xsd:element name="nationality" type="xsd:string"/>
        <xsd:element name="gender" type="xsd:string"/>
    </xsd:all>
</xsd:complexType>

1 个答案:

答案 0 :(得分:0)

我不得不做个工作。 :

$server->wsdl->addComplexType(
   'test', // the type's name
   'complexType',
   'struct',
   'all',
    '',
    array(
            array('name'=>'firtsname1','type'=>'xsd:string'),
            array('name'=>'firstname2','type'=>'xsd:string')
    ));

必须这样:

$server->wsdl->addComplexType(
   'test', // the type's name
   'complexType',
   'struct',
   'all',
    '',
    array(
           'firstName1' => array('name'=>'firstName2','type'=>'xsd:string'),
           'firstName2' => array('name'=>'firstName2','type'=>'xsd:string')
    )
);

响应中的数组必须采用相同的格式。 这不完全是我想要的,但可以正常工作。