将soap xml响应解析为数组

时间:2017-11-30 11:06:40

标签: php xml parsing curl soap

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

<?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

请任何机构都可以帮忙解决这个问题。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

试试这个。

<?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);