【发布时间】:2021-03-31 13:40:26
【问题描述】:
这里的学生,正在处理需要从页面获取信息的学校项目 用 php 卷曲。我现在有这个代码:
<?php
$curl = curl_init();
$url = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml?5105e8233f9433cf70ac379d6ccc5775";
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($curl);
curl_close($curl);
$xml = simplexml_load_string($data);
print("<pre>".print_r($xml,true)."</pre>");
?>
print 的输出是这样的: output
如何获取货币和汇率等属性?另外,我是否正确地进行了 xml 转换?我应该把它改成 json 吗?
编辑:输出文本
SimpleXMLElement Object
(
[Cube] => SimpleXMLElement Object
(
[Cube] => SimpleXMLElement Object
(
[@attributes] => Array
(
[time] => 2021-03-31
)
[Cube] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => USD
[rate] => 1.1725
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => JPY
[rate] => 129.91
)
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => BGN
[rate] => 1.9558
)
)
)
)
)
)
【问题讨论】:
-
检查 php 文档:php.net/manual/en/simplexmlelement.attributes.php,示例 #1。而且你不需要将它转换为json来获取数据
-
小记 文本数据的图片没有帮助。将其发布为文本并格式化,然后可以对其进行索引,我们可以轻松地使用它来复制/粘贴到答案中,或者上帝禁止在发布之前实际测试答案