[英]Consuming JSON API using PHP
我已经阅读并尝试了一些我在其他帖子中发现的类似问题的代码,但我找不到解决方案。
我正在使用这个 API: https://api.ipma.pt/open-data/forecast/meteorology/cities/daily/1100900.Z466DEEC76ECDF5FCA6D38571F6Z32哪个返回:
{
"owner": "IPMA",
"country": "PT",
"data": [
{
"precipitaProb": "0.0",
"tMin": "12.6",
"tMax": "24.0",
"predWindDir": "N",
"idWeatherType": 3,
"classWindSpeed": 2,
"longitude": "-8.8069",
"forecastDate": "2020-05-22",
"latitude": "39.7473"
},
{
"precipitaProb": "0.0",
"tMin": "13.1",
"tMax": "24.2",
"predWindDir": "N",
"idWeatherType": 5,
"classWindSpeed": 2,
"longitude": "-8.8069",
"forecastDate": "2020-05-23",
"latitude": "39.7473"
},
{
"precipitaProb": "0.0",
"tMin": "12.8",
"tMax": "26.5",
"predWindDir": "NW",
"idWeatherType": 2,
"classWindSpeed": 1,
"longitude": "-8.8069",
"forecastDate": "2020-05-24",
"latitude": "39.7473"
},
{
"precipitaProb": "0.0",
"tMin": "13.4",
"tMax": "26.0",
"predWindDir": "NW",
"idWeatherType": 5,
"classWindSpeed": 1,
"longitude": "-8.8069",
"forecastDate": "2020-05-25",
"latitude": "39.7473"
},
{
"precipitaProb": "3.0",
"tMin": "16.3",
"tMax": "31.7",
"predWindDir": "E",
"idWeatherType": 2,
"classWindSpeed": 2,
"longitude": "-8.8069",
"forecastDate": "2020-05-26",
"latitude": "39.7473"
}
],
"globalIdLocal": 1100900,
"dataUpdate": "2020-05-22T14:31:04"
}
我需要从“数据”数组中获取几个值。
到目前为止,我有这个:
<?php
$api_url = file_get_contents('https://api.ipma.pt/open-data/forecast/meteorology/cities/daily/1100900.json');
$forecastLeiria = json_decode($api_url);
var_dump($forecastLeiria);
?>
返回NULL即使 API 工作正常。
干杯!
我已经尝试了您的代码,并且它在我身上运行良好。 此问题的一个可能原因可能是您的 PHP 配置。 请运行phpinfo()
,滚动到核心部分并检查allow_url_fopen指令是否已打开。 如果关闭,则需要在php.ini配置文件中将其打开。 该指令允许 PHP 脚本打开 Internet 上的公共文件,因此会影响file_get_contents
function 如何处理公共文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.