38

I tried to request the weather from a web service supplying data in JSON format. My PHP request code, which did not succeed was:

$url="http://www.worldweatheronline.com/feed/weather.ashx?q=schruns,austria&format=json&num_of_days=5&key=8f2d1ea151085304102710";
$json = file_get_contents($url);
$data = json_decode($json, TRUE);
echo $data[0]->weather->weatherIconUrl[0]->value;    

This is some of the data that was returned. Some of the details have been truncated for brevity, but object integrity is retained:

{ "data": 
    { "current_condition": 
        [ { "cloudcover": "31",
            ... } ],  
      "request": 
        [ { "query": "Schruns, Austria",
            "type": "City" } ],
      "weather": 
        [ { "date": "2010-10-27",
            "precipMM": "0.0",
            "tempMaxC": "3",
            "tempMaxF": "38",
            "tempMinC": "-13",
            "tempMinF": "9",
            "weatherCode": "113",
            "weatherDesc": [ {"value": "Sunny" } ],
            "weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png" } ],
            "winddir16Point": "N",
            "winddirDegree": "356",
            "winddirection": "N",
            "windspeedKmph": "5",
            "windspeedMiles": "3" }, 
          { "date": "2010-10-28",
            ... },

          ... ]
        }
    }
}
Elzo Valugi
  • 27,240
  • 15
  • 95
  • 114
Mark Henry
  • 2,649
  • 7
  • 40
  • 48

7 Answers7

65

This appears to work:

$url = 'http://www.worldweatheronline.com/feed/weather.ashx?q=schruns,austria&format=json&num_of_days=5&key=8f2d1ea151085304102710%22';
$content = file_get_contents($url);
$json = json_decode($content, true);

foreach($json['data']['weather'] as $item) {
    print $item['date'];
    print ' - ';
    print $item['weatherDesc'][0]['value'];
    print ' - ';
    print '<img src="' . $item['weatherIconUrl'][0]['value'] . '" border="0" alt="" />';
    print '<br>';
}

If you set the second parameter of json_decode to true, you get an array, so you cant use the -> syntax. I would also suggest you install the JSONview Firefox extension, so you can view generated json documents in a nice formatted tree view similiar to how Firefox displays XML structures. This makes things a lot easier.

Elzo Valugi
  • 27,240
  • 15
  • 95
  • 114
Max
  • 15,693
  • 14
  • 81
  • 131
  • I had a similar deeply nested json that was killing me once I converted it to an array. I couldn't figure out the proper way to drill down through the array to get to the item I needed. Seems easy, but as someone fairly new to php, your simple solution was exactly what I needed. Thanks! – Scooter Feb 23 '17 at 15:00
49

If you use the following instead:

$json = file_get_contents($url);
$data = json_decode($json, TRUE);

The TRUE returns an array instead of an object.

25

Try this example

$json = '{"foo-bar": 12345}';

$obj = json_decode($json);
print $obj->{'foo-bar'}; // 12345

http://php.net/manual/en/function.json-decode.php

NB - two negatives makes a positive . :)

zod
  • 12,092
  • 24
  • 70
  • 106
4

Seems like you forgot the ["value"] or ->value:

echo $data[0]->weather->weatherIconUrl[0]->value;
mario
  • 144,265
  • 20
  • 237
  • 291
1

When you json decode , force it to return an array instead of object.

$data = json_decode($json, TRUE); -> // TRUE

This will return an array and you can access the values by giving the keys.

cherankrish
  • 2,004
  • 1
  • 16
  • 11
0

You have to make sure first that your server allow remote connection so that the function file_get_contents($url) works fine , most server disable this feature for security reason.

Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
0

While editing the code (because mild OCD), I noticed that weather is also a list. You should probably consider something like

echo $data[0]->weather[0]->weatherIconUrl[0]->value;

to make sure you are using the weatherIconUrl for the correct date instance.

icedwater
  • 4,701
  • 3
  • 35
  • 50