[英]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.