[英]SOAP envelop parsing with all XMLNS - not working on Magento
我已經嘗試了很多次,但一直無法提出解決方案。 我的問題是:我有一個SOAP信封響應,如下所示...
<soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:header>
<soapenv:body>
<mcu:prescreenusereligibilityresponse xmlns:mcu="http://www.ups.com/XMLSchema/XOLTWS/MCUserEligibility/v1.0">
<common:response xmlns:common="http://www.ups.com/XMLSchema/XOLTWS/Common/v1.0">
<common:responsestatus>
<common:code>1</common:code>
<common:description>Success</common:description>
</common:responsestatus>
</common:response>
<mcu:eligibilitystatuscode>3</mcu:eligibilitystatuscode>
</mcu:prescreenusereligibilityresponse>
</soapenv:body>
</soapenv:header>
</soapenv:envelope>
然后,我在Mac上訪問以下元素:
$ns=array();
$xml=new SimpleXMLElement($string);
foreach($xml->getNamespaces(true) as $key=>$url){
$xml->registerXPathNamespace($key, $url);
$ns[]=strval($url);
}
print_r(strval($xml->children($ns[0])->header->body->children($ns[1])->prescreenusereligibilityresponse->eligibilitystatuscode));
在一個單獨的Linux實例上使用相同的方法,我在print_r
行上收到一條錯誤消息,說最后一個孩子不能為null。 我已經確認這些值是正確的。 我也試過使用$xml->xpath('//mcu:eligibilitystatuscode')
沒有成功。
我真的被卡住了-_-
我使用了替代方法。 DOMDocument()
在將不同的對象節點打印到屏幕后,我注意到xml字符串中的大寫字母不同。 我嘗試將名稱空間的大小寫進行比較,但使用SimpleXML仍然沒有成功。
$doc = new DOMDocument();
$doc->loadXML($result);
if($doc){
foreach($doc->getElementsByTagNameNS('*', '*') as $element){
if($element->tagName=='mcu:EligibilityStatusCode'){
if($element->nodeValue==0){
return true;
}
else{
return false;
}
}
}
return false;
}
else{
return false;
}
上面是工作代碼,結果是soap請求返回的xml。 我的代碼實質上遍歷xml響應中的每個節點。 依次顯示在Magento前端的塊上。
考慮到您說它可以在Mac上運行,而不能在Linux上運行,因此行尾可能是個問題。 嘗試添加它,以在解析XML之前用運行系統的默認行替換所有可能的行尾。
$string = preg_replace('~\R~u', PHP_EOL, $string);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.