How to Convert Soap Response to PHP Array

How to convert SOAP response to PHP Array?

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;

SOAP XML to PHP Array

To access the inner data elements...

$xml = simplexml_load_string($data);
$xml->registerXPathNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
$data = $xml->xpath("//soap:Body");
$innerData = (string)$data[0]->TransferDataString->children("urn:www.blank.com:blank:services:2:0:wsdl")->data;
// Convert data to array
$xml2 = simplexml_load_string(trim($innerData));
$array = json_decode(json_encode($xml2), true);
print_r($array);

You can then process the data either as XML, or convert it to an array.
The first part extracts the soap:Body and then it manipulates that to get at the final inner content.

converting SOAP XML response to a PHP object or array

The hint of bksi is not that wrong, however technically as this is XML you only need to access the namespaced elements properly. This works more easy by using an XPath expression and registering the namspace-uri to your own prefix:

$soap = simplexml_load_string($soapXMLResult);
$soap->registerXPathNamespace('ns1', 'http://ivectorbookingxml/');
$test = (string) $soap->xpath('//ns1:SearchResponse/ns1:SearchResult/ns1:SearchURL[1]')[0];
var_dump($test);

Output:

string(100) "http://www.lowcostholidays.fr/dl.aspx?p=0,8,5,0&date=10/05/2013&duration=15&room1=2,1,0_5®ionid=9"

If you don't want to use XPath, you need to specify the namespace while you traverse, only the children in the namespace of the element itself are available directly if the element itself is not prefixed. As the root element is prefixed you first need to traverse up to the response:

$soap     = simplexml_load_string($soapXMLResult);
$response = $soap->children('http://schemas.xmlsoap.org/soap/envelope/')
->Body->children()
->SearchResponse
;

Then you can make use of the $response variable as you know it:

$test = (string) $response->SearchResult->SearchURL;

because that element is not prefixed. As a more complex result is returned this is probably the best because you can easily access all the response values.

Your question is similar to:

  • Parse CDATA from a SOAP Response with PHP

Maybe the code/descriptions there are helpful, too.

parse the soap xml response to array

Try this.

<?php

$xml = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GenerateAuthPasswordResponse >
<GenerateAuthPasswordResult>abcd-efgh</GenerateAuthPasswordResult>
<ResultCode>1</ResultCode>
</GenerateAuthPasswordResponse>
<GenerateAuthPasswordResponse >
<GenerateAuthPasswordResult>abcd-efgh</GenerateAuthPasswordResult>
<ResultCode>1</ResultCode>
</GenerateAuthPasswordResponse>
</soap:Body>
</soap:Envelope>';

$xml = simplexml_load_string($xml, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/");
$ns = $xml->getNamespaces(true);
$soap = $xml->children($ns['soap']);
$res = $soap->Body->children();

print_r($res);

Parse XML data from PHP SOAP Response

This is the usual problem of having various namespaces which you need to navigate around. The root node defines xmlns:soap so you can use that without having to do anything, so the XPath uses //soap:Body/* to find the element inside the body tag, as xpath() returns a list of matching nodes, use [0] to just pick the only one out.

As the body data is all under a default namespace (defined as xmlns="http://webservice.nada.com/") you can extract all of them using $data->children("http://webservice.nada.com/"). This now allows you to use standard object notation to access the values.

One thing to note is although echo automatically converts it to a string, if you use these values elsewhere - you may need to convert it using (string) as the item is in fact a SimpleXMLElement.

$data = <<< XML
<?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:Body>
<getDefaultVehicleAndValueByVinResponse xmlns="http://webservice.nada.com/">
<getDefaultVehicleAndValueByVinResult>
<Uid>1182699</Uid>
<VehicleYear>2015</VehicleYear>
<MakeCode>47</MakeCode>
</getDefaultVehicleAndValueByVinResult>
</getDefaultVehicleAndValueByVinResponse>
</soap:Body>
</soap:Envelope>
XML;

$xml = simplexml_load_string($data);
$data = $xml->xpath("//soap:Body/*")[0];
$details = $data->children("http://webservice.nada.com/");
echo (string)$details->getDefaultVehicleAndValueByVinResult->VehicleYear;

Convert SOAP response to associative array using SimpleXML

You might want to look at SoapClient class then? It'll save HEAPS of trouble.

Nest XML soap response PHP

So using a function found here Parse text between 2 words that seemed like a common way to search for each parameter I care about in the response and get the full value without working around all of the control characters.

$loanUpdate = $soapClient->__call("METHOD", array($loan_param));

print "<pre>\n";
echo "REQUEST:\n" . str_ireplace('><', ">\n<", $soapClient->__getLastRequest()) . "\n"; //Print the Req to check it
echo "RESPONSE:\n" . str_ireplace('><', ">\n<", $soapClient->__getLastResponse()) . "\n"; //Print the Resp to check it
print "</pre>\n";

function get_string_between($string, $start, $end){
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
} //Function for all the info between tags

//$loanDetail = var_dump($loanUpdate);
$response = $soapClient->__getLastResponse();
$xml = new SimpleXMLElement($response);
$body = $xml->xpath('//SBody');
$haystack = str_ireplace('><', ">\n<", $soapClient->__getLastResponse());

$parsed = get_string_between($haystack, '<acctrefno>', '</acctrefno>');

if(is_string($haystack)){
//echo $haystack;
echo 'LoanNumber should be here: ' . $parsed;
}else {
echo 'its not a string';
}

} catch (SoapFault $fault) {
$error = 1;
}


Related Topics



Leave a reply



Submit