【问题标题】:PHP curl soap - get variablePHP curl soap - 获取变量
【发布时间】:2017-09-17 22:36:00
【问题描述】:

我看过多篇关于这个主题的帖子,但似乎我尝试过的每个人都没有返回变量。我要做的就是返回 SessionIdentifier 和 SessionIdentifierHash。下面是我正在尝试的代码。

$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);

【问题讨论】:

    标签: php arrays xml curl soap


    【解决方案1】:

    我还发现这也有效,所以不确定这是否是更好的方法。

    $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);
    

    【讨论】:

      【解决方案2】:

      问题在于响应中有更多的命名空间元素。因此,您需要在整个迭代过程中继续使用registerXPathNamespace()。不过,可能有更好的方法:

      $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;
      

      结果:

      标识符:265277237 - 哈希 2dSXW81GQxNy1iYzRlLTRjZjItYjYyNS01MmEyZDM5M2RlMjI=

      Demo

      请注意,SessionIdentifierHash 周围有一些空格。不知道是不是这个回复的本意。

      【讨论】:

      • 是的,行得通!我在编辑器中意外添加了额外的空间。所以这是一件好事。
      • 很高兴听到!你为什么删除你的另一条评论?那是一种更好的方法!我不知道为什么我坚持使用simplexml_load_string()DOMDocument 是更好的方法。祝你好运!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-03
      • 1970-01-01
      • 2013-02-20
      • 2016-07-09
      • 1970-01-01
      • 2014-07-21
      相关资源
      最近更新 更多