Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to convert SOAP response to PHP Array?

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 17.1k
    Comment on it

    Converting SOAP response to Array in php.

     

    Dealing with a SOAP client response for a flight booking application or any other, This is how we get response from SOAP below:

    <S:Envelope
        xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
        <S:Body>
            <ns2:transaccionResponse
                xmlns:ns2="http://ws.iatai.com/">
                <respuestaTransaccion>
                    <idTransaccion>94567</idTransaccion>
                    <referencia>3958</referencia>
                    <idEstado>3</idEstado>
                    <nombreEstado>Declinada</nombreEstado>
                    <codigoRespuesta>202</codigoRespuesta>
                    <valor>93815.0</valor>
                    <iva>86815.0</iva>
                    <baseDevolucion>0.0</baseDevolucion>
                    <isoMoneda>COP</isoMoneda>
                    <fechaProcesamiento>24-07-2015 12:18:40 PM</fechaProcesamiento>
                    <mensaje>REJECT</mensaje>
                    <tarjetaRespuesta>
                        <idFranquicia>1</idFranquicia>
                        <nombreFranquicia>VISA</nombreFranquicia>
                        <numeroBin>411111</numeroBin>
                        <numeroProducto>1111</numeroProducto>
                    </tarjetaRespuesta>
                    <procesadorRespuesta>
                        <idProcesador>3</idProcesador>
                    </procesadorRespuesta>
                </respuestaTransaccion>
            </ns2:transaccionResponse>
        </S:Body>
    </S:Envelope>

     

    Now we need to convert the response to array via PHP conversion below:

    <?php
    $xml = file_get_contents($response);
    // SimpleXML seems to have problems with the colon ":" in the <xxx:yyy> response tags, so take them out
    $xml = preg_replace(/(<\/?)(\w+):([^>]*>)/, $1$2$3, $xml);
    $xml = simplexml_load_string($xml);
    $json = json_encode($xml);
    $responseArray = json_decode($json,true);
    ?>

    This is the final Result that we see below:

    Array
    (
        [ns2transaccionResponse] => Array
            (
                [respuestaTransaccion] => Array
                    (
                        [idTransaccion] => 94567
                        [referencia] => 3958
                        [idEstado] => 3
                        [nombreEstado] => Declinada
                        [codigoRespuesta] => 202
                        [valor] => 93815.0
                        [iva] => 86815.0
                        [baseDevolucion] => 0.0
                        [isoMoneda] => COP
                        [fechaProcesamiento] => 24-07-2015 12:18:40 PM
                        [mensaje] => REJECT
                        [tarjetaRespuesta] => Array
                            (
                                [idFranquicia] => 1
                                [nombreFranquicia] => VISA
                                [numeroBin] => 411111
                                [numeroProducto] => 1111
                            )
    
                        [procesadorRespuesta] => Array
                            (
                                [idProcesador] => 3
                            )
    
                    )
    
            )
    
    )

     

 1 Comment(s)

  • Thank you! This code very helped me. only i not use first records (func file_get_contents),  so  my variant : 
     
        $result = $client->__doRequest($request,$sandbox, 'createShipmentOrder',1);	
    
       // SimpleXML seems to have problems with the colon ":" in the <xxx:yyy> response tags, so take them out
       $xml = preg_replace("/(<\/?)(\w+):([^>]*>)/", '$1$2$3', $result);
       $xml = simplexml_load_string($xml);
       $json = json_encode($xml);
       $responseArray = json_decode($json,true);
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: