php 使用 ns 解析 soap 响应

php parse soap response with ns

我有以下肥皂反应

<?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <SOAP-ENV:Body>
    <ns0:READResponse xmlns:ns0="/PELTT01">
      <ns0:st_flag>0</ns0:st_flag>
      <ns0:st_title></ns0:st_title>
      <ns0:pod_date></ns0:pod_date>
      <ns0:pod_time></ns0:pod_time>
      <ns0:pod_name></ns0:pod_name>
      <ns0:web_status>
        <ns0:web_date>date1</ns0:web_date>
        <ns0:web_time>time1</ns0:web_time>
        <ns0:web_station>station1</ns0:web_station>
        <ns0:web_status_title>title1</ns0:web_status_title>
      </ns0:web_status>
      <ns0:web_status>
        <ns0:web_date>date2</ns0:web_date>
        <ns0:web_time>time2</ns0:web_time>
        <ns0:web_station>station2</ns0:web_station>
        <ns0:web_status_title>title2</ns0:web_status_title>
      </ns0:web_status>
      ...
      ...
 <ns0:web_status_counter>5</ns0:web_status_counter>
</ns0:READResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我尝试获取 web_status 个节点,但一切都失败了

尝试 1

$dom = new DOMDocument();
$dom->load($xml_response);
$web_status  = $dom->getElementsByTagName('web_status');

尝试 2

$dom = new DOMDocument();
$dom->load($xml_response);
$xpath = new DOMXPath($dom);
$xpath->registerNamespace("ns0", "/PELTT01");
$web_status = $xpath->query('//ns0:web_status');

任何人都可以帮我解析这个 soap 回复

感谢任何帮助

如果我没理解错的话,您正在寻找与您的尝试 2 接近的东西:

$dom->loadXML($xml_response);#note: NOT just "load"
$xpath = new DOMXPath($dom);
$xpath->registerNamespace("ns0", "/PELTT01");
$web_status = $xpath->query('//ns0:web_status');

foreach ($web_status as $wb)
{
    $cn = $wb->childNodes;
    foreach ($cn as $c){
        echo trim($c->textContent);
        }
}

输出:

        date1
        time1
        station1
        title1
      
        date2
        time2
        station2
        title2