2016-05-13 13 views
1

SOAP响应我有一个SOAP响应如下:如何提取PHP

<?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,和消息。
我试过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

$ xmle1 = SimpleXML_load_string(curl_exec($ ch));如果(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对象更容易获得。