如何从SOAP Response获取值

时间:2015-03-04 13:37:32

标签: php parsing soap

我有像SOAP一样的响应,

<status qwe:type="ns1:AcceptedType" date="2015-03-04"/>

我需要使用PHP在单独的对象中获取类型,日期值。 可以使用getElementsByTagName ??

执行此操作

1 个答案:

答案 0 :(得分:0)

使用下面的代码很容易提取节点值,但我无法将这些属性提取为XML节点。

// get the last response from your response
$soapResponse= $client->__getLastResponse();

// create a new DOMDocument instance and load the returned content (exception block should be added here)
$dom = new DOMDocument;
$dom->loadXML($soapResponse);

// parse the XML for status nodes
$nodes = $dom->getElementsByTagName('status');
foreach ($nodes as $node) {

  // check if the required node attributes exist and output their values
  if ($node->hasAttribute('date') && $node->hasAttribute('type')) {
    $date= $node->getAttribute('date');
    $type= $node->getAttribute('type');
    print $date.'-'.$type.'<br />';
  }
}

如果节点datetype将是status节点的子节点,则以下代码可能会返回所需的XML节点:

foreach ($nodes as $node) {
  if ($node->hasChildNodes()) {
    foreach ($node->childNodes as $child) {
      if ($child->nodeName == 'date') {
        $nodeDate= $child;
      } elseif ($child->nodeName == 'type') {
        $nodeType= $child;
      }
    }
  }

  // check if nodes have been created
  if (isset($nodeDate) && isset($nodeType)) {
    var_dump($nodeDate);
    var_dump($nodeType);
  }