Parsing SOAP respose

I have been trying to parse the below soap response without any joy. I have tried going down the xpath way and now using simplexml_load_string without any joy.

here is my soap response:


<envelope:Envelope xmlns:envelope="http://schemas.xmlsoap.org/soap/envelope/"> <envelope:Header></envelope:Header> <envelope:Body xmlns="SilverpopApi:EngageService.SessionMgmt.Login"> <RESULT xmlns="SilverpopApi:EngageService.SessionMgmt.Login" xmlns:envelope="http://schemas.xmlsoap.org/soap/envelope/"> <SUCCESS>true</SUCCESS> <SESSIONID>F9BA7881819F391EE478ACF9328F6EB5</SESSIONID> <ORGANIZATION_ID>134be9-12c81c26df6-4f4749e15ce6d7a21b02ab08b9b7921c</ORGANIZATION_ID> <SESSION_ENCODING>;jsessionid=F9BA7881819F391EE478ACF9328F6EB5</SESSION_ENCODING> </RESULT> </envelope:Body> </envelope:Envelope>

but my below code does nothing:


$soaoresponce = $client->__getLastResponse();
//echo $soaoresponce->SESSIONID;
//echo htmlentities($soaoresponce);

$strSessionID = simplexml_load_string($soaoresponce);
 foreach($strSessionID as $key){
				 //echo $key;
				 echo $key->RESULT[1]->SUCCESS;
			 }

can anyone correct me.

Almost certain you doing it wrong. The way to parse SOAP response is to use SoapClient class in php. It will take care of all the parsing for you and you get back an object of stdClass

How are you getting the raw xml from the soap service anyway?

I am using the SoapClient class.

this is my code:


public function loginRequest(){
		
				
	$client = new SoapClient('http://wsdl', array("trace"=> 1, 'cache_wsdl' => WSDL_CACHE_NONE, 'soap_version'=>SOAP_1_1));
	
	$error = 0;
    
	try {
			
          $login = $client->Login(array('USERNAME'=>$this->username, 'PASSWORD'=>$this->password));
		 
					
			echo "RESPONSE:\
";
			
			$soaoresponce = $client->__getLastResponse();
//echo $soaoresponce->SESSIONID;
//echo htmlentities($soaoresponce);

$strSessionID = simplexml_load_string($soaoresponce);
 foreach($strSessionID as $key){
                 //echo $key;
                 echo $key->RESULT[1]->SUCCESS;
             }  

		       
	} catch (SoapFault $fault) {
         
			$error = 1;
			print('<pre>');
			echo "GENERAL EXCEPTION:<br />".$fault->faultcode."-".$fault->faultstring."\
<br />";
			
			echo "EXCEPTION:\
";
			print_r($fault);
			
			echo "REQUEST:\
";
			echo htmlentities($client->__getLastRequest());
			echo "<br />";
			
			echo "REQUEST HEADERS:\
";
			echo $client->__getLastRequestHeaders();
			
			
			echo "RESPONSE HEADERS:\
";
			echo $client->__getLastResponseHeaders();
			
			echo "RESPONSE:\
";
			echo htmlentities($client->__getLastResponse());
			
			print('</pre>');
		}
       
	}

OK then the $login is your result object, you can work with it directly, most likely
$login->RESULT will be your result.
try
var_dump($login);

The $client->__getLastResponse(); is usually used for debugging only, not a way to parse xml response.

doing this: $login->RESULT brings nothing back.

doing a var_dump of $login brings this back:


object(stdClass)#3 (4) { ["SUCCESS"]=> bool(true) ["SESSIONID"]=> string(32) "944354FDE258349D03C81119EA481580" ["ORGANIZATION_ID"]=> string(51) "134be9-12c81c26df6-4f4749e15ce6d7a21b02ab08b9b7921c" ["SESSION_ENCODING"]=> string(44) ";jsessionid=944354FDE258349D03C81119EA481580" } 

My issue is how to say echo the value of a specific element. For example I want to output the value of SUCCESS.

got it working using xpath.

after further testing I was able to use $login->SUCCESS directly as well, rather than going down the xpath way. wasted so much time on this

That’s what I was talking about. The whole point of using SoapClient is that it does all the parsing of xml for you and you get an object of stdClass to work with, just access the properties of that object directly.

That’s the advantage of Soap over Rest API - very easy to use from the client side.