2017-02-08 158 views
2

我有一個我試圖解析的SOAP響應,但是我似乎得到一個錯誤。解析SOAP響應PHP

SOAP響應:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
<s:Body> 
<ValidateNewUserResponse xmlns="urn:websitea.com/v2"><ValidateNewUserResult xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Message i:nil="true"/> 
<Status>true</Status> 
<FailureReason>None</FailureReason> 
<IdentityValidationOutcome>0</IdentityValidationOutcome> 
<ValidationIdentifier>112244</ValidationIdentifier> 
</ValidateNewUserResult></ValidateNewUserResponse> 
</s:Body> 
</s:Envelope> 

我曾嘗試以下代碼:

$doc = new DOMDocument(); 
$doc->loadXML($strXml); 
echo $doc->getElementsByTagName('Status')->item(0)->nodeValue; 

這將產生以下的錯誤:

Trying to get property of non-object

我也曾嘗試以下代碼:

$get_xml = str_ireplace(['S-ENV:', 'S:'], '', $strResponse); 
$xml = simplexml_load_string($get_xml); 
$status=((string)$xml->Body->ValidateNewUserResponse->ValidateNewUserResult->Status);echo "<br />"; 

將會產生以下錯誤:

simplexml_load_string(): namespace error : Namespace prefix i for nil on Message is not defined

+0

的可能的複製(http://stackoverflow.com/questions/26467445/domdocument-simple-getelementsbytagname -wont工作) –

回答

0

嘗試以下,對我來說工作正常。

$doc = new DOMDocument(); 
$doc->loadXML($strResponse); 
$result = $doc->getElementsByTagName('Status')->item(0)->nodeValue; 
echo $result; 
-1

或者你可以使用WSDL到php生成器,所以你不必分析XML響應,也不需要生成XML請求,因爲你總是會處理PHP對象。使用基本的本地PHP SoapClient類就是這種情況。您還可以使用WSDL的PHP​​發電機如PackageGenerator,因爲它消耗在我看來

0

點SOAP如果XML字符串損壞或不正確地轉義,你使用雙引號,你會得到錯誤的最好方式。測試你的代碼,以下爲我工作(使用單引號):[?DOM文檔的getElementsByTagName簡單不會工作]

$strXml = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
<s:Body> 
<ValidateNewUserResponse xmlns="urn:websitea.com/v2"><ValidateNewUserResult xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Message i:nil="true"/> 
<Status>true</Status> 
<FailureReason>None</FailureReason> 
<IdentityValidationOutcome>0</IdentityValidationOutcome> 
<ValidationIdentifier>112244</ValidationIdentifier> 
</ValidateNewUserResult></ValidateNewUserResponse> 
</s:Body> 
</s:Envelope>'; 

$doc = new DOMDocument(); 
$doc->loadXML($strXml); 
echo $doc->getElementsByTagName('Status')->item(0)->nodeValue;