使用名称空间 PHP 从 SOAP 输出中获取数据

Get Data from SOAP output with namespaces PHP

我正在尝试从下面的 SOAP 输出中提取 "Auv7cbb9Opa8/lbjVYLqVV03ELE=" 值。但它 returns 是一个空数组。你能帮我解决这个问题吗?

$gotContent ='<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
       <soapenv:Body>
          <ns2:signOnResponse xmlns:ns2="http://www.vx.com/omi">
             <sessionHandle>
                <ns1:handle xmlns:ns1="http://www.vx.com/schemas/OMItypes.xsd">Auv7cbb9Opa8/lbjVYLqVV03ELE=</ns1:handle>
             </sessionHandle>
             <result>
                <ns1:resultId xmlns:ns1="http://www.vx.com/schemas/OMItypes.xsd">admin</ns1:resultId>
                <ns1:resultCode xmlns:ns1="http://www.vx.com/schemas/OMItypes.xsd">0</ns1:resultCode>
                <ns1:resultText xmlns:ns1="http://www.vx.com/schemas/OMItypes.xsd">Success</ns1:resultText>
             </result>
          </ns2:signOnResponse>
       </soapenv:Body>
    </soapenv:Envelope>';

    $xml     = simplexml_load_string($gotContent);


    $xml->registerXPathNamespace('ns2', 'http://www.vx.com/omi');

    $session2=$xml->xpath('//sessionHandle');

    print_r($session2) ;

你得到了这个元素,但是 print_r() 在从 SimpleXMLElement 中得到任何有用的东西方面非常糟糕。但这也是您所追求的水平之上的水平。所以添加 ns1 命名空间(无论如何你都不使用 ns2 命名空间)并将它和 handle 添加到 XPath 表达式中以获取数据

$xml->registerXPathNamespace('ns1', 'http://www.vx.com/schemas/OMItypes.xsd');

$session2=$xml->xpath('//sessionHandle/ns1:handle')[0];

print_r((string)$session2) ;