【问题标题】:How to iterate and loop through this XML response with cUrl and PHP?如何使用 cUrl 和 PHP 迭代和循环这个 XML 响应?
【发布时间】:2020-08-26 23:33:18
【问题描述】:

我需要帮助循环通过使用 PHP cURL 进行 api 调用而收到的复杂 XML 响应。我想我已经尝试了所有可以在 SO 和 PHP docs 中找到的东西。没有任何效果。我需要帮助了解如何做到这一点。例如,如果我想打印管理员地址,我该怎么做?

PHP cURL

$ch = curl_init($connection_details['api_host_port']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $data);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
$response = curl_exec($ch);
//
//
//
//parse xml string into SimpleXML objects
$xml = new SimpleXMLElement($response);
print_r($xml);

完整回复

SimpleXMLElement Object
(
[header] => SimpleXMLElement Object
(
[version] => 0.9
)

[body] => SimpleXMLElement Object
(
[data_block] => SimpleXMLElement Object
(
[dt_assoc] => SimpleXMLElement Object
(
[item] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[key] => attributes
)

[dt_assoc] => SimpleXMLElement Object
(
[item] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[key] => affiliate_id
)

)

[1] => 1
[2] => 2020-08-24 21:55:12
[3] => SimpleXMLElement Object
(
[@attributes] => Array
(
[key] => nameserver_list
)

[dt_array] => SimpleXMLElement Object
(
[item] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[key] => 0
)

[dt_assoc] => SimpleXMLElement Object
(
[item] => Array
(
[0] => ns1.systemdns.com
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[key] => ipaddress
)

)

[2] => 1
)

)

)

[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[key] => 1
)

[dt_assoc] => SimpleXMLElement Object
(
[item] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[key] => ipaddress
)

)

[1] => 2
[2] => ns2.systemdns.com
)

)

)

)

)

)

[4] => SimpleXMLElement Object
(
[@attributes] => Array
(
[key] => tld_data
)

)

[5] => 2021-08-24 21:55:10
[6] => NONE
[7] => 2021-08-24 21:55:10
[8] => SimpleXMLElement Object
(
[@attributes] => Array
(
[key] => contact_set
)

[dt_assoc] => SimpleXMLElement Object
(
[item] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[key] => owner
)

[dt_assoc] => SimpleXMLElement Object
(
[item] => Array
(
[0] => +1.5556667777
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[key] => address2
)

)

[2] => Miami
[3] => John
[4] => NA
[5] => FL
[6] => SimpleXMLElement Object
(
[@attributes] => Array
(
[key] => address3
)

)

[7] => active
[8] => SimpleXMLElement Object
(
[@attributes] => Array
(
[key] => fax
)

)

[9] => 3200 Northwest 67th Avenue
[10] => Doe
[11] => tech@example.com
[12] => US
[13] => 33122
)

)

)

[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[key] => admin
)

[dt_assoc] => SimpleXMLElement Object
(
[item] => Array
(
[0] => Miami
[1] => John
[2] => FL
[3] => NA
[4] => SimpleXMLElement Object
(
[@attributes] => Array
(
[key] => address3
)

)

[5] => active
[6] => 3200 Northwest 67th Avenue
[7] => Doe
[8] => SimpleXMLElement Object
(
[@attributes] => Array
(
[key] => fax
)

)

[9] => tech@example.com
[10] => US
[11] => 33122
[12] => +1.5556667777
[13] => SimpleXMLElement Object
(
[@attributes] => Array
(
[key] => address2
)

)

)

)

)

[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[key] => billing
)

[dt_assoc] => SimpleXMLElement Object
(
[item] => Array
(
[0] => +1.5556667777
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[key] => address2
)

)

[2] => FL
[3] => NA
[4] => Miami
[5] => John
[6] => tech@example.com
[7] => 33122
[8] => US
[9] => SimpleXMLElement Object
(
[@attributes] => Array
(
[key] => address3
)

)

[10] => 3200 Northwest 67th Avenue
[11] => active
[12] => SimpleXMLElement Object
(
[@attributes] => Array
(
[key] => fax
)

)

[13] => Doe
)

)

)

[3] => SimpleXMLElement Object
(
[@attributes] => Array
(
[key] => tech
)

[dt_assoc] => SimpleXMLElement Object
(
[item] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[key] => address2
)

)

[1] => +1.5556667777
[2] => Jane Doe
[3] => Los Angeles
[4] => CA
[5] => Company Name
[6] => SimpleXMLElement Object
(
[@attributes] => Array
(
[key] => fax
)

)

[7] => active
[8] => 555 My Street
[9] => Jane Doe
[10] => SimpleXMLElement Object
(
[@attributes] => Array
(
[key] => address3
)

)

[11] => US
[12] => 77788
[13] => tech@example.com
)

)

)

)

)

)

[9] => 1
[10] => 2020-08-24 21:55:10
[11] => 0
)

)

)

[1] => DOMAIN
[2] => 200
[3] => XCP
[4] => REPLY
[5] => Query Successful
[6] => 1
)

)

)

)

)

每个请求用 $xml->asXML() 编辑:

  0.9
  

 
  
   
    Query Successful
    200
    1
    XCP
    DOMAIN
    REPLY
    
     
      1
      
      
       
        
         
          John
          NA
          US
          +1.5556667777
          Doe
          3200 Northwest 67th Avenue
          
          Miami
          
          active
          tech@example.com
          33122
          
          FL
         
        
        
         
          active
          
          
          Los Angeles
          tech@example.com
          77788
          
          CA
          Company Name
          Jane Doe
          +1.5556667777
          US
          555 My Street
          Jane Doe
         
        
        
         
          +1.5556667777
          US
          NA
          John
          Doe
          3200 Northwest 67th Avenue
          tech@example.com
          Miami
          active
          
          
          33122
          
          FL
         
        
        
         
          33122
          active
          Miami
          
          
          tech@example.com
          FL
          
          John
          NA
          US
          +1.5556667777
          Doe
          3200 Northwest 67th Avenue
         
        
       
      
      
      
       
        
         
          ns1.systemdns.com
          
          1
         
        
        
         
          2
          
          ns2.systemdns.com
         
        
       
      
      2020-08-24 21:55:12
      2021-08-24 21:55:10
      NONE
      2020-08-24 21:55:10
      0
      1
      2021-08-24 21:55:10
     

【问题讨论】:

  • 缩进有助于查看数据结构
  • 你能不能也展示一下echo $xml->asXML();的样子?
  • @GetSet : 我不知道怎么做。
  • @JackFleeting:它返回一个格式良好的 XML 字符串,没有额外的东西。
  • “额外的东西”是什么意思?

标签: php xml curl


【解决方案1】:

使用您在评论中提供的链接中的最后一个示例,如下所示 - 使用 xpath - 应该为您提供管理员信息:

$admin = $xml->xpath('//item[@key="admin"]//item');
foreach($admin as $item) {
echo $item;    
}

输出:

US Admin Example Inc. +1.4165550123x1812 CA Suite 100 active Adams adams@example.com Santa Clara 90210 +1.4165550125 32 Oak Street Adler 

或者获取带有 id 2 的服务器的服务器信息:

$server = $xml->xpath('//item[@key="nameserver_list"]//item[@key="2"]//dt_assoc//item');

foreach($server as $item) {
echo $item;    
}

输出:

24.22.23.28 3 patrick.example.com 

等等

【讨论】:

  • 老兄!你真的救了我的命!
  • 如果我想一次提取一个值而不进行迭代,那么方法是什么?您在回答中提出的这种结构对我来说是新的。
猜你喜欢
  • 2021-02-18
  • 2013-04-06
  • 1970-01-01
  • 2013-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-21
相关资源
最近更新 更多