Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

xml - How to convert SOAP response to PHP Array?

I am unable to convert SOAP response to Array in php.

here is the code

 $response = $client->__doRequest($xmlRequest,$location,$action,1);

here is the SOAP response.

<soap:envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:body>
<searchflightavailability33response xmlns="http://FpwebBox.Fareportal.com/Gateway.asmx">
<searchflightavailability33result>
    &lt;Fareportal&gt;&lt;FpSearch_AirLowFaresRS&gt;&lt;CntKey&gt;1777f5a7-7824-46ce-a0f8-33d5e6e96816&lt;/CntKey&gt;&lt;Currency CurrencyCode="USD"/&gt;&lt;OriginDestinationOptions&gt;&lt;OutBoundOptions&gt;&lt;OutBoundOption segmentid="9W7008V21Feb14"&gt;&lt;FlightSegment etc....
    </searchflightavailability33result>
</searchflightavailability33response>
</soap:body>
</soap:envelope>;

i used the following ways to convert to Array,but i am getting empty output.

1.echo '<pre>';print_r($client__getLastResponse());
2.echo '<pre>';print_r($response->envelope->body->searchflightavailability33response);
3.echo '<pre>';print_r($client->SearchFlightAvailability33($response));
     4.simplexml_load_string($response,NULL,NULL,"http://schemas.xmlsoap.org/soap/envelope/");  

5.echo '<pre>';print_r($client->SearchFlightAvailability33($response));

please advice me.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The following SOAP response structure can be easily converted in an array using a combination of the previous methods. Using only the the function "simplexml_load_string" removing the colon ":" it returned null in some cases.

SOAP Response

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

PHP conversion:

$response = preg_replace("/(</?)(w+):([^>]*>)/", "$1$2$3", $response);
$xml = new SimpleXMLElement($response);
$body = $xml->xpath('//SBody')[0];
$array = json_decode(json_encode((array)$body), TRUE); 
print_r($array);

Result:

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
                        )

                )

        )

)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...