在PHP SOAP中传递用户定义的类型

Nik*_*ola 5 php soap types

我无法理解如何在PHP SOAP调用中传递用户定义的类型.有人可以给我一个提示(或手册的链接)吗?

示例:在我的WSDL文件中,我定义了类型:

<types>
<schema targetNamespace="http://example.com/CustData"
        xmlns="http://www.w3.org/2000/10/XMLSchema">
  <element name="personalInformation">
    <complexType>
      <all>
        <element name="name" type="xsd:string"/>
        <element name="title" type="xsd:string"/>
        <element name="lang" type="xsd:string"/>
      </all>
    </complexType>
  </element>
</schema>
Run Code Online (Sandbox Code Playgroud)

我像这样定义服务响应消息:

<message name='getCustDataResponse'>
<part name='Result' type='xsd:personalInformation'/>
<part name='Result1' type='xsd:string'/>
</message>
Run Code Online (Sandbox Code Playgroud)

缺少的部分是 - 如何在SOAP服务器端初始化答案?

我试着写:

$arrRes['Result']['name'] = 'xxx';
$arrRes['Result']['title'] = 'yyy';
$arrRes['Result']['lang'] = 'zzz';
$arrRes['Result']['hehehehe1'] = 'test1';
$arrRes['Result']['hehehehe2'] = 'test2';
$arrRes['Result']['hehehehe3'] = 'test3';
$arrRes['Result']['hehehehe4'] = 'test4';
$arrRes['Result1'] = 'result1';
$arrRes['blablabla'] = 'hahaha';
return $arrRes;
Run Code Online (Sandbox Code Playgroud)

客户端获取响应,当我var_dump它时,它显示arrRes:

array(2) { ["Result"]=>  array(7) { ["name"]=>  string(3) "xxx" ["title"]=>  string(3) "yyy" ["lang"]=>  string(3) "zzz" ["hehehehe1"]=>  string(5) "test1" ["hehehehe2"]=>  string(5) "test2" ["hehehehe3"]=>  string(5) "test3" ["hehehehe4"]=>  string(5) "test4" } ["Result1"]=>  string(7) "result1" } 
Run Code Online (Sandbox Code Playgroud)

我期望得到一个错误,因为我初始化的数组与我定义的响应消息不匹配.

所以我想我在wsdl中定义的类型根本没有使用 - 因此它必须是wsdl或客户端或服务器代码中的错误.

提前感谢您的建议!!

尼古拉

Kay*_*ose 2

我没有对 php 上的 SOAP 服务器端做太多研究,但这里有一个示例,说明如何将类映射与 php 的 SoapClient 一起使用。我相当确定 SoapServer 的工作方式完全相同。(您甚至可以在服务器/客户端之间共享相同的类映射)。

所以你会有这样的课程:

class PersonalInformation
{
public $name;
public $title:
public $lang;
}
Run Code Online (Sandbox Code Playgroud)

那么您的回复:

function getCustData()
{
    $response = new PersonalInformation;
    $response->name = "Me";
    $response->title = "Hi World";
    $response->lang = "En-US";

    $arrResult = array();
    $arrResult['Result'] = $response;
    $arrResult['Result1'] = 'lol';
    return $arrResult
}
Run Code Online (Sandbox Code Playgroud)

然后只需使用类映射,例如:

$server = new SoapServer('foo?wsdl', classmap=array('personalInformation' => 'PersonalInformation'));
//I'm not sure whether you have to use the classmap on BOTH server/client
$client = new SoapClient('foo?wsdl', classmap=array('personalInformation' => 'PersonalInformation'));
Run Code Online (Sandbox Code Playgroud)

至于不合格响应数据上的错误,我认为 php 并没有真正对响应进行任何验证 - 仅对请求进行验证。