1

I have read and tried some code that I found in other posts with similar issues but I couldn't find a solution.

I'm using this API: https://api.ipma.pt/open-data/forecast/meteorology/cities/daily/1100900.json which returns this:

  {
      "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"
    }

I need to get several values from the "data" array.

So far i have this:

<?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);

?>

Returns NULL even tho the API is working just fine.

Cheers!

Vinay
  • 7,442
  • 6
  • 25
  • 48
devnull
  • 13
  • 4

1 Answers1

1

I have tried your code and it works fine on my end. A possible cause of this problem might be your PHP configuration. Please run phpinfo() , scroll to the Core section and check that that the allow_url_fopen directive is turned on. If it is off, then you need to turn it on in your php.ini configuration file. It is the directive that allows PHP scripts to open public files on the internet and therefore affects how file_get_contents function works with public files.

rayalois22
  • 161
  • 3
  • 7
  • Thanks. I just did, and **allow_url_fopen** is **ON**. Still not working tho. [link](https://imgur.com/QDzAf1q) – devnull May 22 '20 at 17:24