【问题标题】:How to parse this SOAP XML response with PHP simplexml_load_string?如何使用 PHP simplexml_load_string 解析这个 SOAP XML 响应?
【发布时间】:2014-03-31 15:38:21
【问题描述】:

恐怕我不知道如何用名称空间和前缀解析这个 XML。这是我从 Curl 请求中得到的 SOAP 回复:

<soap:envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:body>
    <getproductpricekonfigurationresponse xmlns="https://service.printapi.de/interface/">
      <getproductpricekonfigurationresult>
        <xs:schema id="ProduktPreisKonfiguration" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"></xs:schema>
        <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
          <produktpreiskonfiguration xmlns>
            <produktkonfiguration diffgr:id="ProduktKonfiguration1" msdata:roworder="0" diffgr:haschanges="inserted">
              <idobjekt>4</idobjekt>
              <idformat>30</idformat>
              <ustidentnr>
                <objektpreisnetto>18.77</objektpreisnetto>
                <mwstwert>6.01</mwstwert>
              </ustidentnr>
            </produktkonfiguration>
          </produktpreiskonfiguration>
        </diffgr:diffgram>
      </getproductpricekonfigurationresult>
    </getproductpricekonfigurationresponse>
  </soap:body>
</soap:envelope>

如果我想回显例如“objektpreisnetto”节点,我该怎么做?

编辑:这是我尝试过的一些事情(除其他外):

$xml = simplexml_load_string($soap);

$result = $xml->children('http://schemas.xmlsoap.org/soap/envelope/')
    ->children('https://service.printapi.de/interface/')
    ->getproductpricekonfigurationresult
    ->objektpreisnetto;

echo $result->objektpreisnetto;

这没有返回任何内容,但我确定我没有正确处理孩子部分。

这是我尝试的一个更基本的示例,它也没有返回任何内容:

$xml = simplexml_load_string($soap);
foreach ($xml->children('http://schemas.xmlsoap.org/soap/envelope/') as $child)
{
  echo "Child node: " . $child . "<br>";
}

不应该至少返回一个子节点吗?任何见解将不胜感激!

【问题讨论】:

  • 为什么不使用 SOAP 客户端而不是 simpleXML?您能否展示您尝试过的方法并具体说明哪些方法不起作用?
  • XML 无效:xmlvalidation.com
  • 我没有使用 SOAP 客户端,因为我连接的 SOAP 服务使用的是 HTTP 授权,所以我使用 curl。

标签: php xml soap


【解决方案1】:

SoapClient 通过在 SoapClient 选项中设置 loginpassword 来处理 HTTP 身份验证,您尝试了吗?

然后使用 Wsdl 到 php 生成器来帮助您获取将用作类映射的相应 PHP 类,尝试https://www.wsdltophp.com

因此您将只处理 PHP 对象,而无需解析任何内容。

【讨论】:

  • 据我了解,SoapClient 不处理远程服务器使用的 HTTP 身份验证类型。 (请求本身受用户名/密码保护,而不是 wsdl 文件。)但是我还是尝试了它,但它不起作用。我找到了一些解决此问题的建议,即使用 curl 登录并下载 wsdl 文件的本地副本,并让 SoapClient 处理本地文件而不是受密码保护的远程文件。我现在正在尝试这种解决方法,并将尝试使用您的 wsdltophp 脚本。
【解决方案2】:

我设法让它工作了!

首先我使用 curl 登录并发送 SOAP 请求,如以下答案所述:https://stackoverflow.com/a/7157785/1121877

然后我使用 simplexml_load_string 加载返回的 XML,如下所述:https://stackoverflow.com/a/8275835/1121877

感谢大家分享他们的知识!

【讨论】:

    猜你喜欢
    • 2013-06-06
    • 2016-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    • 2016-07-04
    • 2020-02-07
    相关资源
    最近更新 更多