How to read data from SOAP header in client side (PHP)

JackTheKnife

Limp Gawd
Joined
Mar 27, 2006
Messages
272
I have SOAP response which looks like
Code:
    <soap:Envelope>
        <soap:Header>
            <AuthorizationToken soap:mustUnderstand="1">
                <Token>5c31cca8-8303-4d01-a564-a99569a0963a</Token>
            </AuthorizationToken>
        </soap:Header>
        <soap:Body>
            <AuthenticateResponse>
                <AuthenticateResult>http://www.avectra.com/OnDemand/2005/</AuthenticateResult>
            </AuthenticateResponse>
        </soap:Body>
    </soap:Envelope>

and no clue how to read data from the header (get a token value). I'm using SoapClient from PHP5. Thanks for any help
 
I have SimpleXML but still can't read SOAP:

Code:
  $res = $client->__getLastResponse();

  $xml = simplexml_load_string($res,NULL,NULL,"http://schemas.xmlsoap.org/soap/envelope/");
  $xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
  
 
	foreach($xml->xpath('//AuthorizationToken') as $header)
	{
    	var_export($header->xpath('//Token')); 
	}
 
I have got SimpleXML to work:

Code:
  $res = $client->__getLastResponse();
  $xmlString = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $res);
  $xml = simplexml_load_string($xmlString);
  $token = $xml->soapHeader[0]->AuthorizationToken[0]->Token;
 
Check my second post - somehow is not working

Code:
  $res = $client->__getLastResponse();
	$xml = simplexml_load_string($res);  
	$sxe = new SimpleXMLElement($xml); 
	
	$sxe->registerXPathNamespace('soap', 'http://www.avectra.com/OnDemand/2005/');
	$tk = $sxe->xpath('//Token');
	
	foreach ($tk as $token) {
  	echo $token . "\n";
	}
 
Last edited:
It looks like $xml = simplexml_load_string($res); returns empty $xml (where $res is a SOAP response)
 
Code:
<?php 
$xml = <<<EOD
<xml xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
 <soap:Envelope>
        <soap:Header>
            <AuthorizationToken soap:mustUnderstand="1">
                <Token>5c31cca8-8303-4d01-a564-a99569a0963a</Token>
            </AuthorizationToken>
        </soap:Header>
        <soap:Body>
            <AuthenticateResponse>
                <AuthenticateResult>http://www.avectra.com/OnDemand/2005/</AuthenticateResult>
            </AuthenticateResponse>
        </soap:Body>
    </soap:Envelope>
</xml>
EOD;

$sxe = new SimpleXMLElement($xml);

$result = $sxe->xpath('//Token');

foreach ($result as $token) {
  echo $token;
}
?>
 
Based on your code I have created:

Code:
 $res = $client->__getLastResponse();

	$xml = <<<EOD 
	$res 
	EOD;
	$sxe = new SimpleXMLElement($xml); 
	$tk = $sxe->xpath('//Token');
	foreach ($tk as $token) {
  	echo $token . "\n";
	}

Where $res

Code:
<?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:Header><AuthorizationToken soap:mustUnderstand="1" xmlns="http://www.avectra.com/OnDemand/2005/"><Token>a0577c25-27ea-4532-8701-8292a14da2a1</Token></AuthorizationToken></soap:Header><soap:Body><AuthenticateResponse xmlns="http://www.avectra.com/OnDemand/2005/"><AuthenticateResult>http://www.avectra.com/OnDemand/2005/</AuthenticateResult></AuthenticateResponse></soap:Body></soap:Envelope>

With same result as previous - empty $sxe
 
Last edited:
is $res in your last post the actual response or is the xml in your original post (the one without the inline xmlnl definitions) what you're getting back from the server?

If it's the former, you likely have to use registerXPathNamespace(). The example i posted above works but does not use the same xml as the one defined in your $res variable.
 
Back
Top