How to convert SOAP response to PHP Array? How to convert SOAP response to PHP Array? php php

How to convert SOAP response to PHP Array?


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


I found a perfect solution to parse SOAP response to an Array:

$plainXML = mungXML( trim($soapXML) );$arrayResult = json_decode(json_encode(SimpleXML_Load_String($plainXML, 'SimpleXMLElement', LIBXML_NOCDATA)), true);print_r($arrayResult);// FUNCTION TO MUNG THE XML SO WE DO NOT HAVE TO DEAL WITH NAMESPACEfunction mungXML($xml){    $obj = SimpleXML_Load_String($xml);    if ($obj === FALSE) return $xml;    // GET NAMESPACES, IF ANY    $nss = $obj->getNamespaces(TRUE);    if (empty($nss)) return $xml;    // CHANGE ns: INTO ns_    $nsm = array_keys($nss);    foreach ($nsm as $key)    {        // A REGULAR EXPRESSION TO MUNG THE XML        $rgx        = '#'               // REGEX DELIMITER        . '('               // GROUP PATTERN 1        . '\<'              // LOCATE A LEFT WICKET        . '/?'              // MAYBE FOLLOWED BY A SLASH        . preg_quote($key)  // THE NAMESPACE        . ')'               // END GROUP PATTERN        . '('               // GROUP PATTERN 2        . ':{1}'            // A COLON (EXACTLY ONE)        . ')'               // END GROUP PATTERN        . '#'               // REGEX DELIMITER        ;        // INSERT THE UNDERSCORE INTO THE TAG NAME        $rep        = '$1'          // BACKREFERENCE TO GROUP 1        . '_'           // LITERAL UNDERSCORE IN PLACE OF GROUP 2        ;        // PERFORM THE REPLACEMENT        $xml =  preg_replace($rgx, $rep, $xml);    }    return $xml;} // End :: mungXML()

It will give you a perfect array of SOAP XML Data.


finally i found the solution is

we can get body of the response from SOAP the following ways

example1:

$xml = new SimpleXMLElement($soapResponse);foreach($xml->xpath('//soap:body') as $header) {    $output = $header->registerXPathNamespace('default', 'http://FpwebBox.Fareportal.com/Gateway.asmx');    }

example2:

$doc = new DOMDocument('1.0', 'utf-8');    $doc->loadXML( $soapResponse );    $XMLresults     = $doc->getElementsByTagName("SearchFlightAvailability33Response");    $output = $XMLresults->item(0)->nodeValue;