Search code examples
phpxmlsymfonysoapsimplexml

Retrieve DATA from XML with Simplexml


I am trying to extract values from the XML SOAP response working with Symfony2 and php 5.6 and I've read the doc and tried many methods but still can't get the data

Here is my XML string

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
 <S:Body>
  <ns2:provisioningWithDataResponse xmlns="URL" xmlns:ns2="http://URL">
    <ns2:provisioningReturn>
        <date>2016-02-26T09:17:49.648+01:00</date>
        <messageId>642176832</messageId>
        <returnCode>1</returnCode>
        <returnMessage>Code: 6 Message: SQL query failed </returnMessage>
     </ns2:provisioningReturn>
  </ns2:provisioningWithDataResponse>
</S:Body>
</S:Envelope>

I've tried this to get the data and store it in a session but it seems that $xml is always empty as I always get this error message

Notice: Trying to get property of non-object

Code:

$xml=simplexml_load_string($order_return);
$session->set("Respense",(string)$xml->children('ns2',true)->provisioningReturn[0]->returnMessage);

Any Idea how can I retrieve returnMessage value for example ? thanks


Solution

  • try the code below:

    (string)
    // $xml is the node: S:Envelope
    $xml->children('S', true) // => node: S:Body
        ->children('ns2', true) // => node: ns2:provisioningWithDataResponse
        ->children('ns2', true) // => ns2:provisioningReturn
        ->children() 
        ->returnMessage