我们正在将 RPC/编码网络服务转换为文档/文字/包装。 WSDL(使用 nusoap)已被重写以使用新格式。
我这样使用 PHP SoapClient:
new SoapClient($wsdlUrl, array(
'cache_wsdl' => WSDL_CACHE_NONE,
'trace' => true,
'features' => SOAP_SINGLE_ELEMENT_ARRAYS,
));
相关的 WSDL 部分如下所示(应该遵循 WS-I Basic Profile):
<xsd:complexType name="messages">
<xsd:sequence>
<xsd:element name="item" type="xsd:string" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="some_functionResponseType">
<xsd:all>
<xsd:element name="return" type="tns:messages" form="unqualified"/>
</xsd:all>
</xsd:complexType>
<message name="some_functionResponse">
<part name="parameters" element="tns:some_functionResponseType"/>
</message>
<operation name="some_function">
<input message="tns:some_functionRequest"/>
<output message="tns:some_functionResponse"/>
</operation>
当我提交我的请求时,XML 响应是这样的:
<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope 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/">
<SOAP-ENV:Body>
<some_functionResponse xmlns="urn:toets_nl_wsdl">
<messages xmlns="">
<item>foo</item>
<item>bar</item>
</messages>
</some_functionResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
当我在 PHP 中转储结果对象时,它看起来像这样:
stdClass Object
(
[messages] => stdClass Object
(
[item] => Array // <-- here
(
[0] => foo
[1] => bar
)
)
)
为什么结果树中多了一个元素“item”?当我们还在使用 RPC/encoded 时,这是不存在的。
有没有办法在处理响应时删除该元素?
最佳答案
您的 WSDL 明确指出有一个出现次数不受限制的项目,其中包含字符串(也称为数组)。所以 PHP 只是向您呈现 WSDL 中描述的结构,并由服务器返回。
我看不出有什么问题。不要期望 Soap 与 RPC 一样,只是因为它的作用相同。如果您不希望那里有该项目元素,请更改 WSDL 和服务 - 但这可能比使您的 PHP 客户端代码适应新数据结构更困难。
您甚至使用了 SOAP_SINGLE_ELEMENT_ARRAYS,这是避免检查元素是否真的是数组的好方法。
关于php - 为什么 SoapClient 在文档/文字样式中使用 WSDL 返回带有额外键元素的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17853016/