使用PHP消耗JSON API

问题描述 投票:1回答:1

我阅读并尝试了一些在其他帖子中发现的类似问题的代码,但我无法找到解决方案。

我正在使用这个API。https:/api.ipma.ptopen-dataforecastmeteorologycitiesdaily1100900.json。 其中返回这个。

  {
      "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 json api file-get-contents jsondecoder
1个回答
1
投票

我已经尝试了你的代码,在我这里工作得很好,这个问题的一个可能的原因可能是你的PHP配置。phpinfo() ,滚动到核心部分,并检查是否已将 "核心 "设置为 "我"。允许_url_fopen 指令是否开启。如果它是关闭的,那么你需要在你的 php.ini 它是允许PHP脚本在互联网上打开公共文件的指令,因此影响了 file_get_contents 该函数适用于公共文件。

© www.soinside.com 2019 - 2024. All rights reserved.