【问题标题】:parse the soap xml response to array将soap xml响应解析为数组
【发布时间】:2017-11-30 11:06:40
【问题描述】:

我使用 curl 调用了肥皂服务器,我得到了这样的响应:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <GenerateAuthPasswordResponse xmlns="NepalTelecom.AuthGateway">
            <GenerateAuthPasswordResult>abcd-efgh</GenerateAuthPasswordResult>
            <ResultCode>1</ResultCode>
        </GenerateAuthPasswordResponse>
    </soap:Body>
</soap:Envelope>

当我尝试将soap xml解析为:

$response = $this->SoapModel->soapCall($xml , $this->vas_wsdl_url);
 $obj = simplexml_load_string($response);
 echo $obj;die();

[注意:其中 $response 是上述soap xml中提供的soap响应]

我得到 $obj 作为这样的错误:

Severity: Warning
Message:  simplexml_load_string(): namespace warning : xmlns: URI NepalTelecom.AuthGateway is not absolute

请任何机构帮助解决此问题。 提前谢谢你。

【问题讨论】:

标签: php xml parsing curl soap


【解决方案1】:

试试这个。

<?php

$xml = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <GenerateAuthPasswordResponse >
            <GenerateAuthPasswordResult>abcd-efgh</GenerateAuthPasswordResult>
            <ResultCode>1</ResultCode>
        </GenerateAuthPasswordResponse>
        <GenerateAuthPasswordResponse >
            <GenerateAuthPasswordResult>abcd-efgh</GenerateAuthPasswordResult>
            <ResultCode>1</ResultCode>
        </GenerateAuthPasswordResponse>
    </soap:Body>
</soap:Envelope>';

$xml = simplexml_load_string($xml, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/");
$ns = $xml->getNamespaces(true);
$soap = $xml->children($ns['soap']);
$res = $soap->Body->children();

print_r($res);

【讨论】:

  • 感谢 Alex 回答我的问题以帮助我。我很感激。但很抱歉,我仍然没有得到我需要的东西。我想提取标签之间的值。我尝试了您提供的方式,尽管我已经尝试过这种方式。很抱歉说它不起作用。错误是:simplexml_load_string(): namespace warning : xmlns: URI NepalTelecom.AuthGateway is not absolute
  • 我运行了这个程序,但没有收到任何错误。我得到的答案: SimpleXMLElement Object ( [GenerateAuthPasswordResponse] => Array ( [0] => SimpleXMLElement Object ( [GenerateAuthPasswordResult] => abcd-efgh [ResultCode] => 1 ) [1] => SimpleXMLElement Object ( [GenerateAuthPasswordResult] => abcd-efgh [ResultCode] => 1 ) ) )
  • 好的。可能一定有一些身份验证错误。我根据需要使用 strip_tags 获取特定标签内容的任何方式。谢谢亚历克斯。我得到了我的答案。你帮了我很多,给了我很多想法。
猜你喜欢
  • 1970-01-01
  • 2021-06-07
  • 2016-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-31
  • 2020-02-07
  • 1970-01-01
相关资源
最近更新 更多