【发布时间】: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 字符串,没有额外的东西。
-
“额外的东西”是什么意思?