无法在php中解析SOAP XML响应

时间:2014-10-13 08:00:25

标签: php xml parsing soap

我收到了以下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的新手,发现很难解决这个问题。

由于

1 个答案:

答案 0 :(得分:0)

如果你正在使用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/en/book.soap.php