1

I am trying to request some data using json and the wunderground API.

When I use this code it returns the error "Message: Trying to get property of non-object".

<?php

  $json_string = file_get_contents("http://api.wunderground.com/api/1e6a89f0a3aa092d/alerts/q/zmw:00000.1.16172.json");
  $parsed_json = json_decode($json_string);
  $wColor = $parsed_json->{'alerts'}->{'attribution'};
  $wName = $parsed_json->{'alerts'}->{'wtype_meteoalarm'};
  echo "Severe weather alert ${wColor} expected ${wName} - MORE INFO";

?>

The Data is there and can be viewed here...

http://api.wunderground.com/api/1e6a89f0a3aa092d/alerts/q/zmw:00000.1.16172.json

Yet when I use the almost identical example code snippet from the documentation

<?php
  $json_string = file_get_contents("http://api.wunderground.com/api/1e6a89f0a3aa092d/geolookup/conditions/q/IA/Cedar_Rapids.json");
  $parsed_json = json_decode($json_string);
  $location = $parsed_json->{'location'}->{'city'};
  $temp_f = $parsed_json->{'current_observation'}->{'temp_f'};
  echo "Current temperature in ${location} is: ${temp_f}\n";
?> 

It works absolutely fine! How come I the first request fails to work?

ServerSideSkittles
  • 2,713
  • 10
  • 34
  • 60

3 Answers3

2

$parsed_json->alerts is here a numeric array containing objects:

The var_dump()'ed output of it:

object(stdClass)#1 (2) {
  ["response"]=> ...

  ["alerts"]=>
  array(1) {
    [0]=>
    object(stdClass)#4 (15) {
      ["type"]=>
      string(3) "WRN"
      ...
    }
  }
}

So use:

$wColor = $parsed_json->alerts[0]->attribution;
$wName = $parsed_json->alerts[0]->wtype_meteoalarm;
bwoebi
  • 23,637
  • 5
  • 58
  • 79
  • Thank-you. This was explained very well. – ServerSideSkittles Apr 27 '13 at 19:25
  • @ServerSideSkittles why did you then accept the others answer? I'm confused. – bwoebi Apr 27 '13 at 19:39
  • 1
    @bwoebi: isnt SO about answering questions and helping people? It seems cheap that you are complaining that the OP selected another answer - which happens to be correct too, btw.. – Undefined Variable Apr 27 '13 at 21:01
  • @open_sourse I was only wondering because he has thanked me and accepted the other. I thought he'd made a mistake? – bwoebi Apr 27 '13 at 21:18
  • @bwoebi: Oh people do that all the time in SO, especially when then there multiple answers that are correct. Nobody really questions them, which was why I felt strange reading your comment to the OP. Just because OP acknowledged that your answer was well explained doesnt mean he/she cannot accept another answer as the right one. I say this, because I use So to ask questions all the time, and people ask me the same when I appreciate one answer by accept another.. – Undefined Variable Apr 27 '13 at 21:30
  • @bwoebi: I'm very sorry, I chose the other answer based on that the other answer was posted before your one. Your answer was more thorough and in part, better. The other answer was right and was answered first. I'm not sure on the etiquette here at SO, so correct me if that was wrong to do and I will change it. – ServerSideSkittles Apr 28 '13 at 21:42
1

The problem was in the way you were retrieving the fields. Use:

  $wColor = $parsed_json->alerts[0]->attribution;
  $wName = $parsed_json->alerts[0]->wtype_meteoalarm;
raidenace
  • 12,789
  • 1
  • 32
  • 35
0

Try this

$alert = current($parsed_json->{'alerts'});
$wColor = $alert->{'attribution'};
$wName = $alert->{'wtype_meteoalarm'};
Ziumin
  • 4,800
  • 1
  • 27
  • 34
  • @JanDvorak `current` gives here the first entry of the array (as the array pointer is first 0 if it isn't going to be changed) – bwoebi Apr 27 '13 at 19:01