解析SOAP响应

时间:2020-01-03 20:58:26

标签: php soap simplexml

我一直在花费数小时来尝试分析我无法控制的SOAP响应。我尝试了很多在SO上发现的方法,但都没有运气。

这是我从边缘浏览器获得的响应正文:

<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
<ns1:gXMLQueryResponse xmlns:ns1="urn:com-photomask-feconnect-IFeConnect" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<return xsi:type="xsd:string">&lt;?xml version = &apos;1.0&apos; encoding = &apos;UTF-8&apos;?&gt;
&lt;ROWSET&gt;
   &lt;ROW num=&quot;1&quot;&gt;
      &lt;CUSTOMER_NAME&gt;HITACHI&lt;/CUSTOMER_NAME&gt;
   &lt;/ROW&gt;
&lt;/ROWSET&gt;
</return>
</ns1:gXMLQueryResponse>

</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我正在尝试获取CUSTOMER_NAME的值。

这是我正在使用的代码:


$client = new SoapClient($urla, array('trace' => 1));

try {
    $result = $client->__soapCall("gXMLQuery", $params);

    $response = ($client->__getLastResponse());

    $xml = simplexml_load_string($response);


    $rows = $xml->children('SOAP-ENV', true)->Body->children('ns1', true)->gXMLQueryResponse->return->ROWSET->ROW;

    foreach ($rows as $row)
    {
        $customer = $row->CUSTOMER_NAME;
        echo $customer;

    }



} catch (SoapFault $e) {


}

1 个答案:

答案 0 :(得分:1)

return是一个字符串,需要首先解析它,然后才能使用SimpleXML访问它。

首先,您需要使用html_entity_decode对字符串进行解码,然后可以使用simplexml_load_string加载解码后的字符串:

$return = $xml->children('SOAP-ENV', true)->Body->children('ns1', true)->gXMLQueryResponse->return;

$decodedReturn = html_entity_decode($return, ENT_QUOTES | ENT_XML1, 'UTF-8');
$rowset = simplexml_load_string($decodedReturn);

echo $rowset->ROW->CUSTOMER_NAME;