PHP 如何在 foreach 外部访问 SimpleXMLElement object 变量

PHP How to access SimpleXMLElement object variable outside foreach

如果我有一个位于 foreach 循环内的 SimpleXMLElement object,我如何在循环外访问它?

$auth_tokens = array('tok1', 'tok2', 'tok3', 'tok4');

foreach($auth_tokens as $auth_token) { // 4 iterations in loop
    $response = curl_exec($connection); // API xml response
    $xml = simplexml_load_string($response); // loaded xml into object
}

echo header('content-type: text/xml');
echo $xml->asXML();

.= 无效

[] 无效

更新:

下面是 api xml $response 实际输出的内容。如您所见,因为我正在查询 4 ​​个身份验证令牌,所以每次迭代都是一个新的 xml 响应。当我尝试在循环外访问它时,由于多个 <?xml version="1.0" encoding="UTF-8"?> xml headers.

它无法正常工作

我卡住了。不确定如何将所有 4 个回复合二为一。

更新二:

Nick 帮助我弄清楚了如何将所有 4 个响应放入一个 xml 文档中,但我需要编辑节点名称和值,当 xml 位于SimpleXMLElement object。所以我想我可以先在 object 中处理数据,然后当我完成后,将 object 转换回 xml 并保存。但是在循环外访问 object 时,我 运行 遇到了问题。这是因为有 4 个 object 而不是 1 个,所以 .= 没有保存数据。

这是我 echo $response; 并查看源代码时 API 输出的内容。纯粹xml.

<?xml version="1.0" encoding="UTF-8"?>
<GetOrdersResponse xmlns="urn:ebay:apis:eBLBaseComponents">
  <Timestamp>2018-10-26T16:04:38.375Z</Timestamp>
  <Ack>Success</Ack>
  <Version>1083</Version>
  <Build>E1083_CORE_APIXO_18856776_R1</Build>
  <PaginationResult>
    <TotalNumberOfEntries>1</TotalNumberOfEntries>
  </PaginationResult>
  <OrderArray>
    <Order>
      <OrderID>11306025</OrderID>
      <OrderStatus>Completed</OrderStatus>
      <CreatedTime>2018-10-26T14:38:44.000Z</CreatedTime>
    </Order>
  </OrderArray>
</GetOrdersResponse>
<?xml version="1.0" encoding="UTF-8"?>
<GetOrdersResponse xmlns="urn:ebay:apis:eBLBaseComponents">
  <Timestamp>2018-10-26T16:04:39.049Z</Timestamp>
  <Ack>Success</Ack>
  <Version>1083</Version>
  <Build>E1083_CORE_APIXO_18856776_R1</Build>
  <PaginationResult>
    <TotalNumberOfEntries>3</TotalNumberOfEntries>
  </PaginationResult>
  <OrderArray>
    <Order>
      <OrderID>39168452</OrderID>
      <OrderStatus>Completed</OrderStatus>
      <CreatedTime>2018-10-26T14:38:53.000Z</CreatedTime>
    </Order>
    <Order>
      <OrderID>37219192</OrderID>
      <OrderStatus>Completed</OrderStatus>
      <CreatedTime>2018-10-26T15:36:41.000Z</CreatedTime>
    </Order>
    <Order>
      <OrderID>37198277</OrderID>
      <OrderStatus>Completed</OrderStatus>
      <CreatedTime>2018-10-26T15:35:01.000Z</CreatedTime>
    </Order>
  </OrderArray>
</GetOrdersResponse>
<?xml version="1.0" encoding="UTF-8"?>
<GetOrdersResponse xmlns="urn:ebay:apis:eBLBaseComponents">
  <Timestamp>2018-10-26T16:04:39.454Z</Timestamp>
  <Ack>Success</Ack>
  <Version>1083</Version>
  <Build>E1083_CORE_APIXO_18856776_R1</Build>
  <PaginationResult>
    <TotalNumberOfEntries>2</TotalNumberOfEntries>
  </PaginationResult>
  <OrderArray>
    <Order>
      <OrderID>16283499</OrderID>
      <OrderStatus>Completed</OrderStatus>
      <CreatedTime>2018-10-26T15:13:21.000Z</CreatedTime>
    </Order>
    <Order>
      <OrderID>16283499</OrderID>
      <OrderStatus>Completed</OrderStatus>
      <CreatedTime>2018-10-26T15:36:40.000Z</CreatedTime>
    </Order>
  </OrderArray>
</GetOrdersResponse>
<?xml version="1.0" encoding="UTF-8"?>
<GetOrdersResponse xmlns="urn:ebay:apis:eBLBaseComponents">
  <Timestamp>2018-10-26T16:04:39.782Z</Timestamp>
  <Ack>Success</Ack>
  <Version>1083</Version>
  <Build>E1083_CORE_APIXO_18856776_R1</Build>
  <PaginationResult>
    <TotalNumberOfEntries>1</TotalNumberOfEntries>
  </PaginationResult>
  <OrderArray>
    <Order>
      <OrderID>26348797</OrderID>
      <OrderStatus>Completed</OrderStatus>
      <CreatedTime>2018-10-26T15:40:25.000Z</CreatedTime>
    </Order>
  </OrderArray>
</GetOrdersResponse>

这是我执行 var_dump ($response); 并查看源代码时得到的结果。

string(504) "
<?xml version="1.0" encoding="UTF-8"?>
<GetOrdersResponse xmlns="urn:ebay:apis:eBLBaseComponents">
  <Timestamp>2018-10-26T16:10:49.358Z</Timestamp>
  <Ack>Success</Ack>
  <Version>1083</Version>
  <Build>E1083_CORE_APIXO_18856776_R1</Build>
  <PaginationResult>
    <TotalNumberOfEntries>1</TotalNumberOfEntries>
  </PaginationResult>
  <OrderArray>
    <Order>
      <OrderID>11306025</OrderID>
      <OrderStatus>Completed</OrderStatus>
      <CreatedTime>2018-10-26T14:38:44.000Z</CreatedTime>
    </Order>
  </OrderArray>
</GetOrdersResponse>
"
string(797) "
<?xml version="1.0" encoding="UTF-8"?>
<GetOrdersResponse xmlns="urn:ebay:apis:eBLBaseComponents">
  <Timestamp>2018-10-26T16:10:49.439Z</Timestamp>
  <Ack>Success</Ack>
  <Version>1083</Version>
  <Build>E1083_CORE_APIXO_18856776_R1</Build>
  <PaginationResult>
    <TotalNumberOfEntries>3</TotalNumberOfEntries>
  </PaginationResult>
  <OrderArray>
    <Order>
      <OrderID>39168452</OrderID>
      <OrderStatus>Completed</OrderStatus>
      <CreatedTime>2018-10-26T14:38:53.000Z</CreatedTime>
    </Order>
    <Order>
      <OrderID>37219192</OrderID>
      <OrderStatus>Completed</OrderStatus>
      <CreatedTime>2018-10-26T15:36:41.000Z</CreatedTime>
    </Order>
    <Order>
      <OrderID>37198277</OrderID>
      <OrderStatus>Completed</OrderStatus>
      <CreatedTime>2018-10-26T15:35:01.000Z</CreatedTime>
    </Order>
  </OrderArray>
</GetOrdersResponse>
"
string(798) "
<?xml version="1.0" encoding="UTF-8"?>
<GetOrdersResponse xmlns="urn:ebay:apis:eBLBaseComponents">
  <Timestamp>2018-10-26T16:10:50.001Z</Timestamp>
  <Ack>Success</Ack>
  <Version>1083</Version>
  <Build>E1083_CORE_APIXO_18856776_R1</Build>
  <PaginationResult>
    <TotalNumberOfEntries>3</TotalNumberOfEntries>
  </PaginationResult>
  <OrderArray>
    <Order>
      <OrderID>16283499</OrderID>
      <OrderStatus>Completed</OrderStatus>
      <CreatedTime>2018-10-26T15:13:21.000Z</CreatedTime>
    </Order>
    <Order>
      <OrderID>16283499</OrderID>
      <OrderStatus>Completed</OrderStatus>
      <CreatedTime>2018-10-26T15:36:40.000Z</CreatedTime>
    </Order>
    <Order>
      <OrderID>16275107</OrderID>
      <OrderStatus>Completed</OrderStatus>
      <CreatedTime>2018-10-26T16:09:40.000Z</CreatedTime>
    </Order>
  </OrderArray>
</GetOrdersResponse>
"
string(504) "
<?xml version="1.0" encoding="UTF-8"?>
<GetOrdersResponse xmlns="urn:ebay:apis:eBLBaseComponents">
  <Timestamp>2018-10-26T16:10:50.329Z</Timestamp>
  <Ack>Success</Ack>
  <Version>1083</Version>
  <Build>E1083_CORE_APIXO_18856776_R1</Build>
  <PaginationResult>
    <TotalNumberOfEntries>1</TotalNumberOfEntries>
  </PaginationResult>
  <OrderArray>
    <Order>
      <OrderID>26348797</OrderID>
      <OrderStatus>Completed</OrderStatus>
      <CreatedTime>2018-10-26T15:40:25.000Z</CreatedTime>
    </Order>
  </OrderArray>
</GetOrdersResponse>
"

如果我在 foreach 中使用 $xml = simplexml_load_string($response);,并且 var_dump ($xml);,则输出 4 objects。

object(SimpleXMLElement)#1 (6) {
  ["Timestamp"]=>
  string(24) "2018-10-26T16:32:21.048Z"
  ["Ack"]=>
  string(7) "Success"
  ["Version"]=>
  string(4) "1083"
  ["Build"]=>
  string(28) "E1083_CORE_APIXO_18856776_R1"
  ["PaginationResult"]=>
  object(SimpleXMLElement)#2 (1) {
    ["TotalNumberOfEntries"]=>
    string(1) "1"
  }
  ["OrderArray"]=>
  object(SimpleXMLElement)#3 (1) {
    ["Order"]=>
    object(SimpleXMLElement)#4 (3) {
      ["OrderID"]=>
      string(26) "11306025"
      ["OrderStatus"]=>
      string(9) "Completed"
      ["CreatedTime"]=>
      string(24) "2018-10-26T14:38:44.000Z"
    }
  }
}
object(SimpleXMLElement)#3 (6) {
  ["Timestamp"]=>
  string(24) "2018-10-26T16:32:21.736Z"
  ["Ack"]=>
  string(7) "Success"
  ["Version"]=>
  string(4) "1083"
  ["Build"]=>
  string(28) "E1083_CORE_APIXO_18856776_R1"
  ["PaginationResult"]=>
  object(SimpleXMLElement)#1 (1) {
    ["TotalNumberOfEntries"]=>
    string(1) "4"
  }
  ["OrderArray"]=>
  object(SimpleXMLElement)#2 (1) {
    ["Order"]=>
    array(4) {
      [0]=>
      object(SimpleXMLElement)#4 (3) {
        ["OrderID"]=>
        string(25) "39168452"
        ["OrderStatus"]=>
        string(9) "Completed"
        ["CreatedTime"]=>
        string(24) "2018-10-26T14:38:53.000Z"
      }
      [1]=>
      object(SimpleXMLElement)#5 (3) {
        ["OrderID"]=>
        string(26) "37219192"
        ["OrderStatus"]=>
        string(9) "Completed"
        ["CreatedTime"]=>
        string(24) "2018-10-26T15:36:41.000Z"
      }
      [2]=>
      object(SimpleXMLElement)#6 (3) {
        ["OrderID"]=>
        string(26) "37198277"
        ["OrderStatus"]=>
        string(9) "Completed"
        ["CreatedTime"]=>
        string(24) "2018-10-26T15:35:01.000Z"
      }
      [3]=>
      object(SimpleXMLElement)#7 (3) {
        ["OrderID"]=>
        string(26) "37185515"
        ["OrderStatus"]=>
        string(9) "Completed"
        ["CreatedTime"]=>
        string(24) "2018-10-26T16:24:19.000Z"
      }
    }
  }
}
object(SimpleXMLElement)#2 (6) {
  ["Timestamp"]=>
  string(24) "2018-10-26T16:32:22.529Z"
  ["Ack"]=>
  string(7) "Success"
  ["Version"]=>
  string(4) "1083"
  ["Build"]=>
  string(28) "E1083_CORE_APIXO_18856776_R1"
  ["PaginationResult"]=>
  object(SimpleXMLElement)#3 (1) {
    ["TotalNumberOfEntries"]=>
    string(1) "3"
  }
  ["OrderArray"]=>
  object(SimpleXMLElement)#1 (1) {
    ["Order"]=>
    array(3) {
      [0]=>
      object(SimpleXMLElement)#7 (3) {
        ["OrderID"]=>
        string(26) "16283499"
        ["OrderStatus"]=>
        string(9) "Completed"
        ["CreatedTime"]=>
        string(24) "2018-10-26T15:13:21.000Z"
      }
      [1]=>
      object(SimpleXMLElement)#6 (3) {
        ["OrderID"]=>
        string(26) "16283499"
        ["OrderStatus"]=>
        string(9) "Completed"
        ["CreatedTime"]=>
        string(24) "2018-10-26T15:36:40.000Z"
      }
      [2]=>
      object(SimpleXMLElement)#5 (3) {
        ["OrderID"]=>
        string(26) "16275107"
        ["OrderStatus"]=>
        string(9) "Completed"
        ["CreatedTime"]=>
        string(24) "2018-10-26T16:09:40.000Z"
      }
    }
  }
}
object(SimpleXMLElement)#1 (6) {
  ["Timestamp"]=>
  string(24) "2018-10-26T16:32:22.751Z"
  ["Ack"]=>
  string(7) "Success"
  ["Version"]=>
  string(4) "1083"
  ["Build"]=>
  string(28) "E1083_CORE_APIXO_18856776_R1"
  ["PaginationResult"]=>
  object(SimpleXMLElement)#2 (1) {
    ["TotalNumberOfEntries"]=>
    string(1) "1"
  }
  ["OrderArray"]=>
  object(SimpleXMLElement)#3 (1) {
    ["Order"]=>
    object(SimpleXMLElement)#5 (3) {
      ["OrderID"]=>
      string(26) "26348797"
      ["OrderStatus"]=>
      string(9) "Completed"
      ["CreatedTime"]=>
      string(24) "2018-10-26T15:40:25.000Z"
    }
  }
}

这就是我 echo '<pre>' . var_export($xml, true) . '</pre><br>'; 时得到的输出。我发现这个更容易阅读并且可以清楚地看到它是 4 objects.

SimpleXMLElement::__set_state(array(
   'Timestamp' => '2018-10-26T16:36:41.377Z',
   'Ack' => 'Success',
   'Version' => '1083',
   'Build' => 'E1083_CORE_APIXO_18856776_R1',
   'PaginationResult' => 
  SimpleXMLElement::__set_state(array(
     'TotalNumberOfEntries' => '1',
  )),
   'OrderArray' => 
  SimpleXMLElement::__set_state(array(
     'Order' => 
    SimpleXMLElement::__set_state(array(
       'OrderID' => '11306025',
       'OrderStatus' => 'Completed',
       'CreatedTime' => '2018-10-26T14:38:44.000Z',
    )),
  )),
))


SimpleXMLElement::__set_state(array(
   'Timestamp' => '2018-10-26T16:36:43.083Z',
   'Ack' => 'Success',
   'Version' => '1083',
   'Build' => 'E1083_CORE_APIXO_18856776_R1',
   'PaginationResult' => 
  SimpleXMLElement::__set_state(array(
     'TotalNumberOfEntries' => '4',
  )),
   'OrderArray' => 
  SimpleXMLElement::__set_state(array(
     'Order' => 
    array (
      0 => 
      SimpleXMLElement::__set_state(array(
         'OrderID' => '39168452',
         'OrderStatus' => 'Completed',
         'CreatedTime' => '2018-10-26T14:38:53.000Z',
      )),
      1 => 
      SimpleXMLElement::__set_state(array(
         'OrderID' => '37219192',
         'OrderStatus' => 'Completed',
         'CreatedTime' => '2018-10-26T15:36:41.000Z',
      )),
      2 => 
      SimpleXMLElement::__set_state(array(
         'OrderID' => '37198277',
         'OrderStatus' => 'Completed',
         'CreatedTime' => '2018-10-26T15:35:01.000Z',
      )),
      3 => 
      SimpleXMLElement::__set_state(array(
         'OrderID' => '37185515',
         'OrderStatus' => 'Completed',
         'CreatedTime' => '2018-10-26T16:24:19.000Z',
      )),
    ),
  )),
))


SimpleXMLElement::__set_state(array(
   'Timestamp' => '2018-10-26T16:36:43.783Z',
   'Ack' => 'Success',
   'Version' => '1083',
   'Build' => 'E1083_CORE_APIXO_18856776_R1',
   'PaginationResult' => 
  SimpleXMLElement::__set_state(array(
     'TotalNumberOfEntries' => '4',
  )),
   'OrderArray' => 
  SimpleXMLElement::__set_state(array(
     'Order' => 
    array (
      0 => 
      SimpleXMLElement::__set_state(array(
         'OrderID' => '16283499',
         'OrderStatus' => 'Completed',
         'CreatedTime' => '2018-10-26T15:13:21.000Z',
      )),
      1 => 
      SimpleXMLElement::__set_state(array(
         'OrderID' => '16283499',
         'OrderStatus' => 'Completed',
         'CreatedTime' => '2018-10-26T15:36:40.000Z',
      )),
      2 => 
      SimpleXMLElement::__set_state(array(
         'OrderID' => '16275107',
         'OrderStatus' => 'Completed',
         'CreatedTime' => '2018-10-26T16:09:40.000Z',
      )),
      3 => 
      SimpleXMLElement::__set_state(array(
         'OrderID' => '16258277',
         'OrderStatus' => 'Completed',
         'CreatedTime' => '2018-10-26T16:36:19.000Z',
      )),
    ),
  )),
))


SimpleXMLElement::__set_state(array(
   'Timestamp' => '2018-10-26T16:36:44.157Z',
   'Ack' => 'Success',
   'Version' => '1083',
   'Build' => 'E1083_CORE_APIXO_18856776_R1',
   'PaginationResult' => 
  SimpleXMLElement::__set_state(array(
     'TotalNumberOfEntries' => '1',
  )),
   'OrderArray' => 
  SimpleXMLElement::__set_state(array(
     'Order' => 
    SimpleXMLElement::__set_state(array(
       'OrderID' => '26348797',
       'OrderStatus' => 'Completed',
       'CreatedTime' => '2018-10-26T15:40:25.000Z',
    )),
  )),
))

这就是我在循环中使用 echo header('content-type: text/xml'); echo $xml->asXML(); 时得到的输出。

  <?xml version="1.0" encoding="UTF-8"?>
  <GetOrdersResponse xmlns="urn:ebay:apis:eBLBaseComponents">
    <Timestamp>2018-10-26T16:42:57.532Z</Timestamp>
    <Ack>Success</Ack>
    <Version>1083</Version>
    <Build>E1083_CORE_APIXO_18856776_R1</Build>
    <PaginationResult>
      <TotalNumberOfEntries>1</TotalNumberOfEntries>
    </PaginationResult>
    <OrderArray>
      <Order>
        <OrderID>11306025</OrderID>
        <OrderStatus>Completed</OrderStatus>
        <CreatedTime>2018-10-26T14:38:44.000Z</CreatedTime>
      </Order>
    </OrderArray>
  </GetOrdersResponse>
  <?xml version="1.0" encoding="UTF-8"?>
  <GetOrdersResponse xmlns="urn:ebay:apis:eBLBaseComponents">
    <Timestamp>2018-10-26T16:42:58.218Z</Timestamp>
    <Ack>Success</Ack>
    <Version>1083</Version>
    <Build>E1083_CORE_APIXO_18856776_R1</Build>
    <PaginationResult>
      <TotalNumberOfEntries>4</TotalNumberOfEntries>
    </PaginationResult>
    <OrderArray>
      <Order>
        <OrderID>39168452</OrderID>
        <OrderStatus>Completed</OrderStatus>
        <CreatedTime>2018-10-26T14:38:53.000Z</CreatedTime>
      </Order>
      <Order>
        <OrderID>37219192</OrderID>
        <OrderStatus>Completed</OrderStatus>
        <CreatedTime>2018-10-26T15:36:41.000Z</CreatedTime>
      </Order>
      <Order>
        <OrderID>37198277</OrderID>
        <OrderStatus>Completed</OrderStatus>
        <CreatedTime>2018-10-26T15:35:01.000Z</CreatedTime>
      </Order>
      <Order>
        <OrderID>37185515</OrderID>
        <OrderStatus>Completed</OrderStatus>
        <CreatedTime>2018-10-26T16:24:19.000Z</CreatedTime>
      </Order>
    </OrderArray>
  </GetOrdersResponse>
  <?xml version="1.0" encoding="UTF-8"?>
  <GetOrdersResponse xmlns="urn:ebay:apis:eBLBaseComponents">
    <Timestamp>2018-10-26T16:42:58.861Z</Timestamp>
    <Ack>Success</Ack>
    <Version>1083</Version>
    <Build>E1083_CORE_APIXO_18856776_R1</Build>
    <PaginationResult>
      <TotalNumberOfEntries>4</TotalNumberOfEntries>
    </PaginationResult>
    <OrderArray>
      <Order>
        <OrderID>16283499</OrderID>
        <OrderStatus>Completed</OrderStatus>
        <CreatedTime>2018-10-26T15:13:21.000Z</CreatedTime>
      </Order>
      <Order>
        <OrderID>16283499</OrderID>
        <OrderStatus>Completed</OrderStatus>
        <CreatedTime>2018-10-26T15:36:40.000Z</CreatedTime>
      </Order>
      <Order>
        <OrderID>16275107</OrderID>
        <OrderStatus>Completed</OrderStatus>
        <CreatedTime>2018-10-26T16:09:40.000Z</CreatedTime>
      </Order>
      <Order>
        <OrderID>16258277</OrderID>
        <OrderStatus>Completed</OrderStatus>
        <CreatedTime>2018-10-26T16:36:19.000Z</CreatedTime>
      </Order>
    </OrderArray>
  </GetOrdersResponse>
  <?xml version="1.0" encoding="UTF-8"?>
  <GetOrdersResponse xmlns="urn:ebay:apis:eBLBaseComponents">
    <Timestamp>2018-10-26T16:42:59.253Z</Timestamp>
    <Ack>Success</Ack>
    <Version>1083</Version>
    <Build>E1083_CORE_APIXO_18856776_R1</Build>
    <PaginationResult>
      <TotalNumberOfEntries>1</TotalNumberOfEntries>
    </PaginationResult>
    <OrderArray>
      <Order>
        <OrderID>26348797</OrderID>
        <OrderStatus>Completed</OrderStatus>
        <CreatedTime>2018-10-26T15:40:25.000Z</CreatedTime>
      </Order>
    </OrderArray>
  </GetOrdersResponse>

我需要的是更改所有 <OrderID></OrderID> 节点以显示 <Test></Test> 并附加到其中的值,我用这个

做到了
foreach($auth_tokens as $key => $auth_token) {

    $responses = curl_exec($connection);
    $xml = simplexml_load_string($responses);
    curl_close($connection);

    $entries = $xml->PaginationResult->TotalNumberOfEntries;
    $xml = $xml->OrderArray->Order;

    if($entries == 0) {
        continue;
    } else {

        foreach($xml as $key => $val) {

            $val->Test = substr($val->OrderStatus, 0, 3) . $val->OrderID; // changes the <OrderID> node from <OrderID> to <Test> and appends the first 3 charachters from <OrderStatus> value to the <Test> value
            unset($val->OrderID); // deletes the now empty <OrderID> node

        } // end foreach

    }

echo header('content-type: text/xml');
echo $xml->asXML();

} // end foreach

这输出正是我需要的,除了它只需要一个 parent 节点环绕它并且它只从每个响应输出 1 个订单。

<Order>
  <OrderStatus>Completed</OrderStatus>
  <CreatedTime>2018-10-26T15:36:41.000Z</CreatedTime>
  <Test>Com372191926</Test>
</Order>
<Order>
  <OrderStatus>Completed</OrderStatus>
  <CreatedTime>2018-10-26T15:13:21.000Z</CreatedTime>
  <Test>Com16283499</Test>
</Order>
<Order>
  <OrderStatus>Completed</OrderStatus>
  <CreatedTime>2018-10-26T15:40:25.000Z</CreatedTime>
  <Test>Com26348797</Test>
</Order>

但我似乎无法围绕此获得 parent 节点,因为我无法成功保存此数据并在 foreach 循环之外访问它。我很接近!

您可以尝试这样的操作,将响应聚合到一个 XML 字符串中,然后在转换之前将其包装在根元素中:

$auth_tokens = array('tok1', 'tok2', 'tok3', 'tok4');
$xmlstr = '';
foreach($auth_tokens as $auth_token) { // 4 iterations in loop
    $response = curl_exec($connection); // API xml response
    $xmlstr .= $response;               // add response to XML
}
$xml = simplexml_load_string("<Orders>$xmlstr</Orders>");
header('content-type: text/xml');
echo $xml->asXML();

更新

如果 XML 响应包含 <?xml ... > headers,则上述代码不起作用。快速而肮脏的解决方法是使用 preg_replace 将它们删除,即将 $xmlstr .= $response; 更改为

$xmlstr .= preg_replace('/<\?xml[^>]*>/', '', $response);

Demo

"correct" 修复是单独处理 XML 响应并将它们作为 children 添加到根文档。这可以使用 DomDocument:

来完成
$xml = new DomDocument();
$xml->loadXML('<Orders></Orders>');
$xmlroot = $xml->documentElement;
$respdoc = new DomDocument();
foreach($auth_tokens as $auth_token) {   // 4 iterations in loop
    $response = curl_exec($connection); // API xml response
    $respdoc->loadXML($response);        // create a domDocument
    $resproot = $respdoc->documentElement;   // get the root element
    $resproot = $xml->importNode($resproot, true); // import into XML
    $xmlroot->appendChild($resproot);
}
header('content-type: text/xml');
echo $xml->saveXML();

Demo

我想通了这个问题。我需要在子 foreach 循环中连接 .=,而不是在父循环中。然后我就可以访问循环外的数据了。而且我必须使用 $val->asXML() 将数据保存到一个变量中。请注意下面代码中的注释行。

foreach($auth_tokens as $key => $auth_token) { // 4 iterations in loop

    $responses = curl_exec($connection); // API xml response
    $xml = simplexml_load_string($responses); // loaded xml into SimpleXMLElement object
    curl_close($connection);

    $entries = $xml->PaginationResult->TotalNumberOfEntries;
    $xml = $xml->OrderArray->Order; // Just get the Order elements

    if($entries == 0) {
        continue;
    } else {

        foreach($xml as $key => $val) {

            $val->Test = substr($val->OrderStatus, 0, 3) . $val->OrderID; // changes the <OrderID> node from <OrderID> to <Test> and appends the first 3 charachters from <OrderStatus> value to the <Test> value
            unset($val->OrderID); // deletes the <OrderID> node

            $orders .= $val->asXML(); // concatenate here in child loop instead of parent foreach

        } // end child foreach

    }

} // end parent foreach

$xml = new DomDocument('1.0', 'utf-8');
$xml->preserveWhiteSpace = false;
$xml->formatOutput = true;
$xml->loadXML('<Orders>' . $orders . '</Orders>'); // able to add my parent node <orders></Orders>
$xml->save('test.xml', LIBXML_NOEMPTYTAG); // saves xml to file

header('content-type: text/xml');
echo $xml->saveXML(); // saves xml for display

输出正是我需要的,没有太多开销,而且格式也很好。

<?xml version="1.0"?>
<Orders>
  <Order>
    <OrderStatus>Completed</OrderStatus>
    <CreatedTime>2018-10-26T15:36:41.000Z</CreatedTime>
    <Test>Com10789</Test>
  </Order>
  <Order>
    <OrderStatus>Completed</OrderStatus>
    <CreatedTime>2018-10-26T15:13:21.000Z</CreatedTime>
    <Test>Com10790</Test>
  </Order>
  <Order>
    <OrderStatus>Completed</OrderStatus>
    <CreatedTime>2018-10-26T15:40:25.000Z</CreatedTime>
    <Test>Com141570</Test>
  </Order>
  <Order>
    <OrderStatus>Completed</OrderStatus>
    <CreatedTime>2018-10-26T15:36:41.000Z</CreatedTime>
    <Test>Com141571</Test>
  </Order>
  <Order>
    <OrderStatus>Completed</OrderStatus>
    <CreatedTime>2018-10-26T15:13:21.000Z</CreatedTime>
    <Test>Com141572</Test>
  </Order>
  <Order>
    <OrderStatus>Completed</OrderStatus>
    <CreatedTime>2018-10-26T15:36:41.000Z</CreatedTime>
    <Test>Com19992</Test>
  </Order>
  <Order>
    <OrderStatus>Completed</OrderStatus>
    <CreatedTime>2018-10-26T15:40:25.000Z</CreatedTime>
    <Test>Com19993</Test>
  </Order>
</Orders>