php - 为什么 SoapClient 在文档/文字样式中使用 WSDL 返回带有额外键元素的数组?

标签 php soap wsdl

我们正在将 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/

相关文章:

php - 添加自定义验证消息以在 Laravel 5.3 中自定义验证不起作用

php - 我应该如何通过 AJAX 发出长 PHP 请求,定期检查状态更新,并在请求取消时关闭脚本?

java - 如何使用 Apache cxf 为安全(https)soap Web 服务生成 java 类

ruby-on-rails - 如何在 Rails 3.1 中创建 SOAP API Web 服务

java - JBoss:如何从 WSDL 生成 Web 服务?

web-services - 使用 Objective C 进行 SOAP 查询

javascript - Google 基于 MySQL 和 PHP 结果以不同颜色绘制条形图

php - 函数 mcrypt_encrypt() 在 Wamp 中不起作用(PHP 版本 5.2.6)

python - 为什么我不能在 pysimplesoap 中设置 SOAP header ?

java - 内部 Web 服务的最佳实践