在 PHP SOAP 中传递用户定义的类型 [英] Passing user-defined types in PHP SOAP
问题描述
我无法理解如何在 PHP SOAP 调用中传递用户定义的类型.有人能给我一个提示(或手册的链接)吗?
I'm having trouble understanding how to pass user-defined types in PHP SOAP calls. Could someone give me a hint (or a link to a manual) please?
示例:在我的 WSDL 文件中,我定义了类型:
Example: In my WSDL file, I define the type:
<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>
我这样定义服务响应消息:
I define the service response message like this:
<message name='getCustDataResponse'>
<part name='Result' type='xsd:personalInformation'/>
<part name='Result1' type='xsd:string'/>
</message>
缺少的部分是 - 如何在 SOAP 服务器端初始化答案?
The missing part is - how do I initialize the answer on the SOAP server side?
我试着写:
$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;
客户端得到响应,当我 var_dump 它时,它显示了 arrRes:
The client gets the response back and when I var_dump it, it shows the 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" }
我预计会收到错误消息,因为我初始化的数组与我定义的响应消息不匹配.
I expected to get an error because the array I initialized doesn't match the response message I have defined.
所以我猜我在 wsdl 中定义的类型根本没有使用 - 所以它一定是 wsdl 或客户端或服务器代码中的错误.
So I guess the type I have defined in the wsdl isn't used at all - so it must be an error either in the wsdl or in the client or server code.
预先感谢您的建议!!
尼古拉
推荐答案
我在 php 上的 SOAP 服务器端做的不多,但是 这里是如何使用类映射和php的SoapClient的示例.我相当确定 SoapServer 的工作方式完全相同.(您甚至可以在服务器/客户端之间共享相同的类映射).
I haven't done much with the server-side of SOAP on php, but here is an example of how to use class mapping with php's SoapClient. I am fairly certain that SoapServer works the exact same way. (You could probably even share the same class map between server/client).
所以你会有一个这样的类:
So you would have a class like this:
class PersonalInformation
{
public $name;
public $title:
public $lang;
}
然后是您的回复:
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
}
然后只需使用类映射,如:
Then just use a class map like:
$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'));
至于不符合响应数据的错误,我认为 php 并没有真正对响应进行任何验证 - 只有请求.
As far as the errors on non-conforming response data, I don't think php really does any validation on the reponse - only the request.
这篇关于在 PHP SOAP 中传递用户定义的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!