How do I read a soap response with simplexml_load_string()

I am trying to parse a soap response with simplexml_load_string(). I have my soap client set with trace = 1 and exceptions = 0 and $client->__getLastResponse() gives me this result:

<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV = "http://schemas.xmlsoap.org/soap/envelope/"   SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<MDHeader>
<userid>inspector@prime.com</userid>
<password>prime123456</password>
<batchid>1234</batchid></MDHeader>
<RECORDSET>
<ROW id='0'>
<INSPECTIONS>
<FOLDER_ID>835410936</FOLDER_ID>
<FOLDER_ID>835221706</FOLDER_ID>
<FOLDER_ID>835222299</FOLDER_ID>
</INSPECTIONS>
</ROW>
</RECORDSET>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

I then read the results into $xml:

$xml = "<?xml version='1.0' encoding='UTF-8'?>";
$xml .= urldecode($client->__getLastResponse());

An finally try to echo an element with no luck:

$xml1 = simplexml_load_string($xml,null,null,'http://schemas.xmlsoap.org/soap/envelope/',true);
echo "ELEMENT:" . $xml1->Envelope->Body->MDHeader->userid;

I believe it is the namespace SOAP-ENV that is giving me the issue but I don't know how to resolve it.

You can see my test page at: http://www.primevaluationservices.com/myriad/test.php

Answers


I think you don't need this line because it is already in the xml:

$xml = "<?xml version='1.0' encoding='UTF-8'?>";

To get the 'userid', I think you can use the SimpleXMLElement children method:

$source = <<<SOURCE
<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV = "http://schemas.xmlsoap.org/soap/envelope/"   SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<MDHeader>
<userid>inspector@prime.com</userid>
<password>prime123456</password>
<batchid>1234</batchid></MDHeader>
<RECORDSET>
<ROW id='0'>
<INSPECTIONS>
<FOLDER_ID>835410936</FOLDER_ID>
<FOLDER_ID>835221706</FOLDER_ID>
<FOLDER_ID>835222299</FOLDER_ID>
</INSPECTIONS>
</ROW>
</RECORDSET>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
SOURCE;
$xml1 = simplexml_load_string($source);

echo $xml1->children('SOAP-ENV', true)->Body->children('')->MDHeader->userid->__toString();

Or the SimpleXMLElement xpath method:

$elements = $xml1->xpath('//SOAP-ENV:Envelope/SOAP-ENV:Body/MDHeader/userid');
$userid = $elements[0]->__toString();
echo '<br>';
echo $userid;

Will both result in:

inspector@prime.com

Demo


Need Your Help

How to build software that doesn't require a framework on the user's machine?

frameworks

I am an ASP.NET developer, but now I want to build a software that can be installed on my PC. Software built in .NET platform only works when the .NET Framework is installed, and software written i...

How to use external mic on Android?

android audio-recording microphone

How to use external mic instead of the phone's mic for recording?