编程语言
首页 > 编程语言> > 使用php建立对complexType序列的SOAP请求的正确方法是什么:SoapClient?

使用php建立对complexType序列的SOAP请求的正确方法是什么:SoapClient?

作者:互联网

我很难理解SoapClient是如何工作的.

WDSL

<wsdl:definitions xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
  <wsdl:types>
    <s:schema elementFormDefault="qualified" targetNamespace="[redacted]">
      <s:element name="GetResult">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="Foo" type="s:string" />
            <s:element minOccurs="0" maxOccurs="1" name="Bar">
              <s:complexType mixed="true">
                <s:sequence>
                  <s:any />
                </s:sequence>
              </s:complexType>
            </s:element>
        </s:complexType>
      </s:element>
      [..]
    </s:schema>
  </wsdl:types>
  [..]
</wsdl:definitions>

Web服务的所有者建议请求必须如下所示:

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
    <soap:Body>
        <GetResult xmlns="[redacted]">
            <Bar>
                <Bar>
                    <Qux>bar_qux_value</Qux>
                </Bar>
            </Bar>
            <Foo>foo_string</Operation>
        </GetResult>
    </soap:Body>
</soap:Envelope>

我认为我应该做的是:

$client = new \SoapClient($url_to_wsdl);
$result = $client->GetResult([
    "Foo" => "foo_value",
    "Bar" => [
        "Bar" => [
            "Qux" => "bar_qux_value"
        ]
    ]
]);

但这给了我:

SOAP-ERROR: Encoding: object has no ‘any’ property

那么也许我需要这样做?

$client = new \SoapClient($url_to_wsdl);
$result = $client->GetResult([
    "Foo" => "foo_value",
    "Bar" => [
        "any" => [
            "Bar" => [
                "Qux" => "bar_qux_value"
            ]
        ]
    ]
]);

但这构建了如下请求:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Body>
        <ns1:GetResult>
            <ns1:Foo>foo_value</ns1:Operation>
            <ns1:Bar>bar_qux_value</ns1:Bar>
        </ns1:GetResult>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Bar的值已减少为字符串,服务器不接受该字符串.

经过多次尝试后,我发现此请求有效:

$client = new \SoapClient($url_to_wsdl);
$result = $client->GetResult([
    "Foo" => "foo_value",
    "Bar" => [
        "any" => [ "<ns1:Bar><ns1:Qux>bar_qux_value</ns1:Qux></ns1:Bar>" ]
    ]
]);

我有几个问题:

>为什么Bar> Bar> Qux不在wsdl中?
>为什么有两个级别的Bar?这只是设计不佳吗?
>为什么我需要< any>节点?
>有没有办法构建请求而不必内联一些xml?

解决方法:

您好在本地测试并检查您的错误使用SOAP-UI从您的wsdl创建一个soap服务器模拟并调用它并检查日志错误.

对不起我英文.
祝好运 !

标签:php,soap,soap-client
来源: https://codeday.me/bug/20190710/1424396.html