我有一个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>
我需要提取faultcode,faultstring和message 我已经尝试过SimpleXML_load_string,SimpleXMLElement,DOMDocument,registerXPathNamespace和json_decode但似乎无法获得正确的过程,因为我得到错误而不是结果。提前致谢。 最新尝试:
$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;
}
答案 0 :(得分:0)
<?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
您可能希望编写一个类来解析XML字符串并使用方法构建一个很好的Fault
对象,以便在解析它之后更方便地访问它。