Read Xml Soap from a variable string php

1

I have a variable that collects the answer SOAP of the BBDD , and I need to analyze its content instead of making another request and consuming time. I'm implementing something like a "cache" and avoiding requests soap continuous.

My code:

$xml = simplexml_load_string($resultCache['respuesta_xml']);
echo "<textarea>";
print_r($xml);
echo "</textarea>";

The xml is this:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Header>
        <OutputHeader xmlns="Tras.WS.ExternalGateway">
            <ExternalID />
            <ProcessID></ProcessID>
            <Language>1</Language>
            <Version>1</Version>
        </OutputHeader>
    </soap:Header>
    <soap:Body>
        <ListadoDisponibilidad xmlns="Tras.WS.ExternalGateway">
            <ListadoDisponibilidadResult>
                <Journeys xmlns="Tras.WS.Interfaces">
                    <Journey>

                    </Journey>
                </Journeys>
                <Error xmlns="Tras.WS.Interfaces">
                    <ErrorCode>0</ErrorCode>
                    <ErrorDescription />
                </Error>
            </ListadoDisponibilidadResult>
        </ListadoDisponibilidad>
    </soap:Body>
</soap:Envelope>

But I get this error:

  

exception 'ErrorException' with message 'simplexml_load_string ():   namespace warning: xmlns: URI Tras.WS.ExternalGateway is not   absolute '

    
asked by 06.06.2017 в 13:04
source

1 answer

1

I propose the following PHP function to transform XML to an array:

function xml2array($xmlObject, $out == array())
{
   foreach( (array) $xmlObject as $index => $node)
      $out[$index] = ( is_object ($node) ) ? $xml2array ($node) : $node;
   return $out;
}

You use it like this:

Try to use it 1 time and do DEBUG WITH PHP

$nuevoArray = xml2array(simplexml_load_string($resultCache['respuesta_xml']));
dd($nuevoArray);

If at this point you still see an OBJECT instead of an array, you pass it 2 times through the function.

$nuevoArray = xml2array(xml2array(simplexml_load_string($resultCache['respuesta_xml'])));
dd($nuevoArray);

The reason to apply it 2 times is because sometimes you need it depends on how the content comes. I for a request that I make some data for a web project that I have, I use it and it works perfectly for me.

I advise you to use DEBUG DE PHP "dd ()" to see how the data looks and learn the difference between "OBJECT" < - > "ARRAY" And you will see that the XML come in the form of object php .

    
answered by 06.06.2017 / 13:43
source