读取SOAP,XML的响应

时间:2017-09-20 17:55:42

标签: php xml soap

我在解决下面的SOAP响应方面遇到了问题,我认为这是由于"部分...

任何人都可以告诉我一个例子,我可以参考" DSPTrackingNumber" ?

   $xml_resp = "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
       <soap:Body>
       <ns2:GenerateLabelResponse 
       xmlns:ns2='http://cpgm.dpdhl.com/returnit/ws/generate_label'>
       <label>base64</label>
       <DSPTrackingNumber>409388775152</DSPTrackingNumber>
       <responseText>Label generated.</responseText>
       <responseCode>0</responseCode>
       </ns2:GenerateLabelResponse>
       </soap:Body>
       </soap:Envelope>";

编辑:我试过了。

$xml = simplexml_load_string($xml_resp);

$xml->registerXPathNamespace("ns2", "http://cpgm.dpdhl.com/returnit/ws/generate_label");

$RespData = $xml->xpath("//ns2");

echo "DSPTrackingNumber=".$RespData->DSPTrackingNumber;

1 个答案:

答案 0 :(得分:0)

如果您只是想要DSPTrackingNumber,那么您不需要使用命名空间,因为它没有在元素中使用。

所以 -

$xml_resp = "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
       <soap:Body>
       <ns2:GenerateLabelResponse
       xmlns:ns2='http://cpgm.dpdhl.com/returnit/ws/generate_label'>
       <label>base64</label>
       <DSPTrackingNumber>409388775152</DSPTrackingNumber>
       <responseText>Label generated.</responseText>
       <responseCode>0</responseCode>
       </ns2:GenerateLabelResponse>
       </soap:Body>
       </soap:Envelope>";

$xml = simplexml_load_string($xml_resp);

//$xml->registerXPathNamespace("ns2", "http://cpgm.dpdhl.com/returnit/ws/generate_label");

$RespData = $xml->xpath("//DSPTrackingNumber");

echo "DSPTrackingNumber=".$RespData[0];

...给出

DSPTrackingNumber=409388775152