处理SOAP响应 - 命名空间

时间:2013-09-05 23:48:30

标签: php xml soap simplexml

<soap:envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:body>
        <acceptleadresponse xmlns="http://tempuri.org/WhiteBox/Lead">
            <acceptleadresult>
                <purchase>false</purchase>
                <rejectreason>Duplicate Lead</rejectreason>
                <tier>0</tier>
                <starttime>2013-09-05T18:15:32.1757337-05:00</starttime>
                <endtime>2013-09-05T18:15:32.2693339-05:00</endtime>
                <redirect>https://www1.paydaymate.com.au/Account/Sorry</redirect>
            </acceptleadresult>
        </acceptleadresponse>
    </soap:body>
</soap:envelope>

我需要获得两个节点,重定向和购买。但是所有的价值观都会很好(foreach)。我尝试过使用命名空间,但实际上我没有得到任何结果

$xml = simplexml_load_string($response['response'], NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/");
//print_r($xml);
$xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');

foreach($xml->xpath('//acceptleadresponse') as $header){

    var_export($header->xpath('//acceptleadresult'));

}

1 个答案:

答案 0 :(得分:0)

使用SimpleXML执行此操作非常简单:)

$xml = simplexml_load_string($xml);
$xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');

$result = $xml->xpath('/soap:envelope/soap:body');
$ar = $result[0];

$result = $ar->acceptleadresponse->acceptleadresult;
print_r($result);

$ result现在将包含:

   SimpleXMLElement Object
   (
       [purchase] => false
       [rejectreason] => Duplicate Lead
       [tier] => 0
       [starttime] => 2013-09-05T18:15:32.1757337-05:00
       [endtime] => 2013-09-05T18:15:32.2693339-05:00
       [redirect] => https://www1.paydaymate.com.au/Account/Sorry
   )