繁体   English   中英

无法解析PHP中的SOAP XML响应

[英]Cannot parse SOAP XML response in php

我收到以下XML响应

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://api.testingabc.in">
<SOAP-ENV:Body>
   <ns1:LoginResponse>
       <return>
           <SessionID>DEMO1234</SessionID>
           <ResponseCode>0</ResponseCode>
           <ResponseMessage>Successful</ResponseMessage>
       </return>
   </ns1:LoginResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我努力了

            $response = curl_exec($soap_do);
            $xml = simplexml_load_string($response, NULL, NULL,"http://schemas.xmlsoap.org/soap/envelope/");
            $ns = $xml->getNamespaces(true);
            $soap = $xml->children($ns['SOAP-ENV']);
            $res = $soap->Body->children($ns['ns1']);
            print_r($res);

$response will have the xml response.when I execute this Im getting 

SimpleXMLElement Object ( [LoginResponse] => SimpleXMLElement Object ( ) )

但是现在我需要获取SessionID,ResponseCode,ResponseMessage如何获取它,我需要访问LoginResponse中的对象。

请帮忙。

我是php的新手,发现很难解决它。

谢谢

如果您使用curl,则需要从响应中删除SOAP标记以将其解析为xml,而不是像以下这样的好方法解析:

$str ='<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://api.testingabc.in">
<SOAP-ENV:Body>
   <ns1:LoginResponse>
       <return>
           <SessionID>DEMO1234</SessionID>
           <ResponseCode>0</ResponseCode>
           <ResponseMessage>Successful</ResponseMessage>
       </return>
   </ns1:LoginResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>';
$szString = str_replace(array('<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://api.testingabc.in">
<SOAP-ENV:Body>
   <ns1:LoginResponse>','</ns1:LoginResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>'), '', $str);
print_r(simplexml_load_string($szString));

输出:-

SimpleXMLElement Object ( [SessionID] => DEMO1234 [ResponseCode] => 0 [ResponseMessage] => Successful ) 

因此最好使用SOAP调用方法来获取响应并使用soap函数解析soap响应。 有关SOAP的更多信息,请阅读手册: -http : //php.net/manual/zh/book.soap.php

暂无
暂无

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

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