简体   繁体   English

使用所有XMLNS进行SOAP信封解析-无法在Magento上使用

[英]SOAP envelop parsing with all XMLNS - not working on Magento

I have tried and tried and have been unable to come up with a solution. 我已经尝试了很多次,但一直无法提出解决方案。 My issue is this: I have a SOAP envelop response which is as follows... 我的问题是:我有一个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>

I then access the elements like this on my mac: 然后,我在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));

Using the same method on a separate Linux instance I get an error on the print_r line, saying the last child cannot be null. 在一个单独的Linux实例上使用相同的方法,我在print_r行上收到一条错误消息,说最后一个孩子不能为null。 I have confirmed that the values are correct. 我已经确认这些值是正确的。 I have also tried using $xml->xpath('//mcu:eligibilitystatuscode') with no success. 我也试过使用$xml->xpath('//mcu:eligibilitystatuscode')没有成功。

I'm really stuck -_- 我真的被卡住了-_-

I used an alternative. 我使用了替代方法。 DOMDocument()

I noticed after printing out the different object nodes to the screen, the capitalization in the xml string was different. 在将不同的对象节点打印到屏幕后,我注意到xml字符串中的大写字母不同。 I tried comparing the namespaces with proper capitalization and still had no success using SimpleXML. 我尝试将名称空间的大小写进行比较,但使用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;
            }

Above is the working code, where result is the xml returned from the soap request. 上面是工作代码,结果是soap请求返回的xml。 My code essentially cycles through each node in the xml response. 我的代码实质上遍历xml响应中的每个节点。 Which in turn displays on a block on the Magento front end. 依次显示在Magento前端的块上。

Considering that you say it works on your Mac and not on Linux, it is possible that the line endings are an issue. 考虑到您说它可以在Mac上运行,而不能在Linux上运行,因此行尾可能是个问题。 Try adding this to replace all possible line endings with the default for the system you are running on prior to parsing the XML. 尝试添加它,以在解析XML之前用运行系统的默认行替换所有可能的行尾。

$string = preg_replace('~\R~u', PHP_EOL, $string);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM