如何在PHP中提取SOAP响应

如何在PHP中提取SOAP响应,php,soap,response,Php,Soap,Response,我的SOAP响应如下: <?xml version="1.0" ?> <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> <S:Body> <S:Fault xmlns:ns4="http://www.w3.org/2003/05/soap-envelope" xmlns=""> <faultcode>stuff</faultcode> <f

我的SOAP响应如下:

<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<S:Fault xmlns:ns4="http://www.w3.org/2003/05/soap-envelope" xmlns="">
<faultcode>stuff</faultcode>
<faultstring>stuff</faultstring>
<detail>
<ns2:Exception xmlns:ns2="http://blah.com/">
<message>stuff</message>
</ns2:Exception>
</detail>
</S:Fault>
</S:Body>
</S:Envelope>
您可能希望编写一个类来解析XML字符串,并使用方法构建一个漂亮的
Fault
对象,以便在解析后更容易访问它


您可能希望编写一个类来解析XML字符串,并使用方法构建一个漂亮的
Fault
对象,以便在解析后更易于访问。

您可以共享您尝试过的代码吗?$xmle1=SimpleXML\u load\u string(curl\u exec($ch));if(curl_errno($ch)){print“curl error:[”.curl_error($ch)。“]”;}else{$xmle1->registerXPathNamespace('thing1',');foreach($xml->xpath('//thing1:message')as$item){echo(string)$item;}请不要将代码转储到评论中。编辑您的原始帖子以添加新信息添加了上述最新尝试。您可以共享您尝试过的代码吗?$xmle1=SimpleXML_加载_字符串(curl_exec($ch));if(curl_errno($ch)){.print“curl error:[”.curl_error($ch)。“]”}。else{$xmle1->registerXPathNamespace('thing1',');foreach($xml->xpath($xpath)('//thing1:message')作为$item){echo(string)$item;}请不要将代码转储到评论中。请编辑您的原始帖子以添加新信息上面添加的最新尝试。
$xmle1 = SimpleXML_load_string(curl_exec($ch));
if (curl_errno($ch)) {
 print "curl error: [" . curl_error($ch) . "]";
} else {
 $xmle1->registerXPathNamespace('thing1', 'http://blah.com/');
 foreach ($xml->xpath('//thing1:message') as $item) {
 echo (string) $item;
}
<?php
$string = <<<XML
<?xml version="1.0" ?>
<S:Envelope
    xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
        <S:Fault
            xmlns:ns4="http://www.w3.org/2003/05/soap-envelope"
            xmlns="">
            <faultcode>stuff</faultcode>
            <faultstring>stuff</faultstring>
            <detail>
                <ns2:Exception
                    xmlns:ns2="http://blah.com/">
                    <message>stuff</message>
                </ns2:Exception>
            </detail>
        </S:Fault>
    </S:Body>
</S:Envelope>
XML;

$_DomObject = new DOMDocument;
$_DomObject->loadXML($string);
if (!$_DomObject) {
    echo 'Error while parsing the document';
    exit;
}

$s = simplexml_import_dom($_DomObject);

foreach(['faultcode','faultstring','message'] as $tag){
        echo $tag.' => '.$_DomObject->getElementsByTagName($tag)[0]->textContent.'<br/>';
}

?>
faultcode => stuff
faultstring => stuff
message => stuff