首页 > 解决方案 > 从 php CURL 读取 SOAP XML

问题描述

嗨,我有这个来自肥皂服务的 xml。我用 curl 获取 xml。php中如何访问节点?结果可以有更多的resulSets

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <ns3:ExampleResponse xmlns:ns3="http://example.com/ptfall" xmlns:ns2="http://example.com/soa">
            <return>
                <ns3:resultSet>
                    <ns3:categoria>
                        <ns2:codice>1</ns2:codice>
                        <ns2:descrizione>Esempio xxx</ns2:descrizione>
                    </ns3:categoria>
                    <ns3:causale>
                        <ns3:codice>_XXXXX</ns3:codice>
                        <ns3:descrizione>Annullo Mancato</ns3:descrizione>
                        <ns3:identificativo>
                            <ns2:long>74</ns2:long>
                        </ns3:identificativo>
                    </ns3:causale>
                    
                </ns3:resultSet>
               
                <ns3:serviceInfo>
                    <ns2:codiceErroreOccorso>0</ns2:codiceErroreOccorso>
                    <ns2:erroreOccorso>false</ns2:erroreOccorso>
                    <ns2:executionId>xxxxxxxxxxx</ns2:executionId>
                    <ns2:tipoErroreOccorso>0</ns2:tipoErroreOccorso>
                </ns3:serviceInfo>
            </return>
        </ns3:ExampleResponse>
    </soap:Body>
</soap:Envelope>

例如,我想为每个我尝试使用的结果集只返回 ns3:descrizione 节点,但没有用

$soap = simplexml_load_string($data);
$response = $soap->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children()->ExampleResponse->........DUNNO HOW TO DO IT;
echo $response;

标签: phpxmlsoap

解决方案


它比这更复杂一些,但是 - 对于你的例子ns3:descrizione- 它可以通过这种方式完成:

$soap = simplexml_load_string($data);
$soap->registerXPathNamespace("ns3", "http://example.com/ptfall");
$resultSet = $soap->xpath('//ns3:resultSet');

foreach ($resultSet as $rs)
{   
    $infos  = $rs->xpath('.//*[local-name()="descrizione"]/text()');
    foreach ($infos as $target) {
        echo $target ."\r\n";
    }   
}

输出:

Esempio xxx
Annullo Mancato

推荐阅读