处理 SOAP 响应 - 命名空间


Handling SOAP response - Namespaces

<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'));
}

使用 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
   )