Converting Soap Xml Response to a PHP Object or Array

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.

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;

Convert SOAP XML response to PHP object or array

The location of the WSDL-file is going to depend on the SOAP API you're calling. Check their documentation.

The example code you provided of implies they're working locally, and you're hosting the WSDL on your own, local machine.

Research Edit

Apparently you can use the PHP soap client in non-WSDL mode. This is accomplished by passing in null as the param instead of the location of the wsdl file.

Also relevant, is this other SO result on non-WSDL mode

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

PHP - converting XML to array in PHP - parsing a soap xml in php and storing it in database

The best solution would be to use PHP's SoapClient class to do the call which will return you an object and then converting this object to an array, like so:

<?php
$client = new SoapClient("http://localhost/code/soap.wsdl");

// Soap call with HelloWorld() method
$something = $client->HelloWorld(array('option1' => 'attribute1'));

// Convert object to array
$array = (array)$something;

?>

Which you can then store in the database.

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.



Related Topics



Leave a reply



Submit