簡體   English   中英

解析SOAP響應PHP

[英]Parsing SOAP response PHP

我有一個要解析的SOAP響應,但是似乎出現錯誤。

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;

這將產生以下錯誤:

試圖獲取非對象的屬性

我還嘗試了以下代碼:

$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():名稱空間錯誤:未定義Message上的nil的名稱空間前綴i

嘗試以下方法,對我來說效果很好。

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

如果XML字符串已損壞或未正確轉義,並且使用了雙引號,則會出現錯誤。 測試您的代碼,以下代碼對我有用(使用單引號):

$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;

或者,您可以使用WSDL到php生成器,這樣您就不必解析XML響應或生成XML請求,因為您將始終處理PHP對象。 使用基本的本機PHP SoapClient類就是這種情況。 您還可以使用WSDL來生成諸如PackageGenerator之類的php生成器,因為從我的角度來看,這是使用SOAP的最佳方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM