【发布时间】:2020-08-22 18:08:17
【问题描述】:
我向网站发出 cURL 请求,返回的响应是 XML 格式的。请求已成功完成,资源已成功创建。我习惯用 json 访问数据,但是当返回的响应是 XML 时,我如何访问数据/值?
PHP
$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);
header('Content-type: application/xml');
print_r($response);
回应
<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<!DOCTYPE OPS_envelope SYSTEM 'ops.dtd'>
<OPS_envelope>
<header>
<version>0.9</version>
</header>
<body>
<data_block>
<dt_assoc>
<item key="protocol">XCP</item>
<item key="action">REPLY</item>
<item key="object">DOMAIN</item>
<item key="is_success">1</item>
<item key="response_code">200</item>
<item key="response_text">
Domain registration successfully completed. WHOIS Privacy successfully enabled. Domain successfully locked.
</item>
<item key="attributes">
<dt_assoc>
<item key="admin_email">jsmith@example.com</item>
<item key="whois_privacy_state">enabled</item>
<item key="registration_text">Domain registration successfully completed. WHOIS Privacy successfully enabled. Domain
successfully locked.</item>
<item key="registration_code">200</item>
<item key="id">3735281</item>
<item key="cancelled_orders">
<dt_array>
<item key="0">3764860</item>
<item key="1">3764861</item>
</dt_array>
</item>
</dt_assoc>
</item>
</dt_assoc>
</data_block>
</body>
</OPS_envelope>
【问题讨论】:
-
使用
simplexml_load_string()然后xpath()- php.net/manual/en/book.simplexml.php