2016-05-13 15 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對象更容易獲得。