1

I have seen multiple posts on this subject but it seems everyone I have tried doesn't return a variable. All I'm trying to do is return the SessionIdentifier and SessionIdentifierHash. Below is the code I am trying.

$soapresults = <<< LOL
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <ActivityId CorrelationId="09c896dd-59d9-4cdf-82db-41b8ffed6400"
    xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">ded007e7-c496-4ec6-8596-4a302f12b446</ActivityId>
  </s:Header>
  <s:Body>
    <ResponseOf_PublicLoginResult xmlns="http://schemas.sendwordnow.com/ws/2010/05/PublicSessionManager">
      <OperationResult xmlns:a="http://schemas.datacontract.org/2004/07/SWN.Notification.PublicSessionManagement.Contracts.DataContracts"
      xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <a:IsSuccess>true</a:IsSuccess>
        <a:ResultCode>Success</a:ResultCode>
        <a:ResultDescription>Successfull login.</a:ResultDescription>
        <a:SessionToken xmlns:b="http://schemas.datacontract.org/2004/07/SWN.Framework.SessionState">
          <b:SessionIdentifier>265277237</b:SessionIdentifier>
          <b:SessionIdentifierHash>
          2dSXW81GQxNy1iYzRlLTRjZjItYjYyNS01MmEyZDM5M2RlMjI=</b:SessionIdentifierHash>
        </a:SessionToken>
        <a:Username>myapiusername</a:Username>
        <a:PartnerId>1</a:PartnerId>
        <a:PasswordExpiredAfter>-1</a:PasswordExpiredAfter>
        <a:QuickSendExpiredAfter>-1</a:QuickSendExpiredAfter>
      </OperationResult>
    </ResponseOf_PublicLoginResult>
  </s:Body>
</s:Envelope>
LOL;
$soap = simplexml_load_string($soapresults);
$soap->registerXPathNamespace('ns1', 'http://schemas.sendwordnow.com/ws/2010/05/PublicSessionManager');
$test = (string) $soap->xpath('//ns1:OperationResult/ns1:IsSuccess');
var_dump($test);

2 Answers 2

1

I also found this that also works so not sure if this is a better way.

$soap_response = $soapresults;
$dom_result = new DOMDocument;
if (!$dom_result->loadXML($soap_response))
    throw new Exception(_('Error parsing response'), 11);
    $my_val = $dom_result->getElementsByTagName('SessionIdentifier')->item(0)->nodeValue;
var_dump($my_val);
Sign up to request clarification or add additional context in comments.

Comments

0

The problem is that there are more namespaced elements in the response. So, you need to keep using registerXPathNamespace() throughout the iteration. There's probably a better way, though:

$soap = simplexml_load_string($soapresults);
$soap->registerXPathNamespace('ns1', 'http://schemas.sendwordnow.com/ws/2010/05/PublicSessionManager');
$test = $soap->xpath('//ns1:OperationResult');
foreach ($test as $el) {
    $el->registerXPathNamespace('ns2', 'http://schemas.datacontract.org/2004/07/SWN.Notification.PublicSessionManagement.Contracts.DataContracts');
    $el2 = $el->xpath("//ns2:SessionToken");
    foreach ($el2 as $val) {
        $val->registerXPathNamespace('ns3', 'http://schemas.datacontract.org/2004/07/SWN.Framework.SessionState');
        $sessionIdentifier = $val->xPath("//ns3:SessionIdentifier")[0];
        $sessionIdentifierHash = $val->xPath("//ns3:SessionIdentifierHash")[0];
    }
}
echo "Identifier: ".$sessionIdentifier." - Hash: ". $sessionIdentifierHash;

Result:

Identifier: 265277237 - Hash 2dSXW81GQxNy1iYzRlLTRjZjItYjYyNS01MmEyZDM5M2RlMjI=

Demo

Notice that the SessionIdentifierHash has some whitespace around it. I don't know if this is the intention of the response.

2 Comments

Yep that works! I added the extra space by accident in my editor. So that's a good to go.
Glad to hear! Why did you delete your other comment? That was a way better way to go at it! I don't know why I stuck with simplexml_load_string(), DOMDocument was a better way. Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.