【发布时间】:2021-08-08 21:00:56
【问题描述】:
我正在尝试使用 PHP 从以下 URL (https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&hoursBeforeNow=3&mostRecent=true&stationString=KORL) 提取信息,但由于某种原因,我一直没有收到任何信息。
搜索了一下,终于找到了
<?php
$url = 'https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&hoursBeforeNow=1&stationString=KORL';
$xml = json_decode(file_get_contents($url));
echo $xml;
?>
编辑:上面的代码显然是错误的......我更新的(仍然是错误的)代码如下。
$url = 'https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&hoursBeforeNow=3&mostRecent=true&stationString=KORL';
$xml = simplexml_load_file($url) or die("feed not loading");
$string_data = $xml;
$xmlstr = simplexml_load_string($string_data);
$data = (string) $xmlstr->data->METAR->raw_text;
echo $data;
我需要从中获取的信息是<raw_text>。
非常感谢您的帮助!
【问题讨论】:
-
为什么
json_decode()是一个 XML 文件?file_get_contents($url)本身是否有效? -
@brombeer 所以我已经尝试了一切......我提供的示例可能不是最好的,因为它是我最后一次尝试,因为在某些情况下可以将 XML 读取为 JSON .我最近的尝试是:
$url = 'https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&hoursBeforeNow=3&mostRecent=true&stationString=KORL'; $xml = simplexml_load_file($url) or die("feed not loading"); $string_data = $xml; $xmlstr = simplexml_load_string($string_data); $data = (string) $xmlstr->data->METAR->raw_text; echo $data; -
现在您尝试解析它两次。每次你第一次使用一个函数时,你应该花一点时间查看它的手册页并了解它的确切作用。试图从名称中猜测并不总是微不足道的。