user3101337
user3101337

Reputation: 364

Parsing a SOAP response

I have been spending hours trying to parse a SOAP response that I have no control over. I have tried numerous methods I found on SO with no luck.

Here is the response body I get from edge browser:

<?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>

I'm trying to get the CUSTOMER_NAME value.

Here is the code I am using:


$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) {


}

Upvotes: 1

Views: 391

Answers (1)

lbrandao
lbrandao

Reputation: 4373

return is a string, you need to parse it first before you can access it using SimpleXML.

First you need to decode the string using html_entity_decode, after that you can load the decoded string with 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;

Upvotes: 1

Related Questions