This question already has an answer here:
I'm trying to parse the following SOAP response, and need some guidance:
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header/>
<env:Body>
<ns2:LookupResponse xmlns:ns2="http://path-to/schemas">
<ns2:Name>My Name</ns2:Name>
<ns2:Address1>test</ns2:Address1>
<ns2:Address2>test</ns2:Address2>
...
</ns2:LookupResponse>
</env:Body>
</env:Envelope>
I retreive the response via cURL:
$url = 'https://path-to-service';
$success = FALSE;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: text/xml; charset=utf-8',
'Content-Length: ' . strlen($request)
));
$ch_result = curl_exec($ch);
$ch_error = curl_error($ch);
curl_close($ch);
I'm new to all of this, so forgive obvious errors, but I am then trying to iterate through the response as an object, as parsed by the simpleXML extension, referencing the SO answer here and using the simplexml_debug plugin to echo object content.
if(empty($ch_error))
{
$xml = simplexml_load_string($ch_result, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/");
$xml ->registerXPathNamespace('env', 'http://schemas.xmlsoap.org/soap/envelope/');
$xml ->registerXPathNamespace('ns2', 'http://path-to/schemas');
echo '<pre>';
simplexml_dump($xml);
echo '</pre>';
}
else
{
echo 'error';
show($ch_error);
exit;
}
This gives me the following:
SimpleXML object (1 item)
[
Element {
Namespace: 'http://schemas.xmlsoap.org/soap/envelope/'
Namespace Alias: 'env'
Name: 'Envelope'
String Content: ''
Content in Namespace env
Namespace URI: 'http://schemas.xmlsoap.org/soap/envelope/'
Children: 2 - 1 'Body', 1 'Header'
Attributes: 0
}
]
I want to get to the stage where I can iterate through the Body of the XML document, using either a foreach
loop, or merely pointing directly to the relevant data ($title = (string)$data->title;
). How can I proceed from my current position to get to that stage? I don't really know what comes next, and I just flat-out don't understand the documentation provided for the SOAP extension in PHP. I would rather use 'basic' code to achieve what I need.
</div>