Help Returning 3 Day Forecast Info with PHP

  • 1
  • Question
  • Updated 7 years ago
Using PHP, how can I access the 3 day forecast information? I would like to display high/low temperatures, and the text forecast. I haven't been able to figure out which XML values to return in the section of the XML.

Everything I have tried so far has not worked. I know how to return current conditions, but not the forecast info.

Any help provided will be greatly appreciated. Thanks!
Photo of mboltonwx

mboltonwx

  • 12 Posts
  • 0 Reply Likes

Posted 7 years ago

  • 1
Photo of Globe199

Globe199

  • 14 Posts
  • 1 Reply Like
Here's the code I developed this week:


$QueryForecast = file_get_contents("[forecast url]");

$f = 0;
$ForecastArray = array();
while ($QueryForecastDecode->{'forecast'}->{'simpleforecast'}->{'forecastday'}[$f]) {
$Forecasts = $QueryForecastDecode->{'forecast'}->{'simpleforecast'}->{'forecastday'}[$f];
$ForecastArray[$f] = array(
$Forecasts->{'date'}->{'weekday_short'},
$Forecasts->{'high'}->{'fahrenheit'},
$Forecasts->{'low'}->{'fahrenheit'},
$Forecasts->{'conditions'},
$Forecasts->{'icon_url'},
$Forecasts->{'pop'}
);
$f++;
}


This makes an array of arrays. Try outputting $Forecasts[0][1] -- this example would be for Day 0 (today) and the high temp for that day.

btw, we REALLY need the ability to format code in this forum.
Photo of mboltonwx

mboltonwx

  • 12 Posts
  • 0 Reply Likes
When I output $Forecasts[0][1] using an echo command, I received error messages about undefined variables. If I'm understanding this correctly, shouldn't an echo work to define the variables?

This is what I have:



<?php
$QueryForecast = file_get_contents("http://api.wunderground.com/api/0e89f92d515673b5/geolookup/conditions/forecast/q/autoip.json");
$f = 0;
$ForecastArray = array();
while ($QueryForecastDecode->{'forecast'}->{'simpleforecast'}->{'forecastday'}[$f]) {
$Forecasts = $QueryForecastDecode->{'forecast'}->{'simpleforecast'}->{'forecastday'}[$f];
$ForecastArray[$f] = array(
$Forecasts->{'date'}->{'weekday_short'},
$Forecasts->{'high'}->{'fahrenheit'},
$Forecasts->{'low'}->{'fahrenheit'},
$Forecasts->{'conditions'},
$Forecasts->{'icon_url'},
$Forecasts->{'pop'}
);
$f++;
}
?>

<DOCTYPE html>

<html>

<body>

<?php echo "${'Forecasts[0][1]'}"; ?>

</body>
</html>
Photo of Globe199

Globe199

  • 14 Posts
  • 1 Reply Like
You're close. The syntax would be:


echo $ForecastArray[0][1];


I prefer print() instead of echo(), but you can also do this if you're simply outputting a var:

http://php.net/manual/en/function.ech...

Look for the shortcut syntax.
Photo of mboltonwx

mboltonwx

  • 12 Posts
  • 0 Reply Likes
Ok, thanks. Now, I have:



<?php
$QueryForecast = file_get_contents("http://api.wunderground.com/api/0e89f92d515673b5/geolookup/conditions/forecast/q/autoip.json");
$f = 0;
$ForecastArray = array();
while ($QueryForecastDecode->{'forecast'}->{'simpleforecast'}->{'forecastday'}[$f]) {
$Forecasts = $QueryForecastDecode->{'forecast'}->{'simpleforecast'}->{'forecastday'}[$f];
$ForecastArray[$f] = array(
$Forecasts->{'date'}->{'weekday_short'},
$Forecasts->{'high'}->{'fahrenheit'},
$Forecasts->{'low'}->{'fahrenheit'},
$Forecasts->{'conditions'},
$Forecasts->{'icon_url'},
$Forecasts->{'pop'}
);
$f++;
}
?>

<DOCTYPE html>

<html>

<body>

<?php echo $ForecastArray[0][1]; ?>

</body>
</html>


It is still, unfortunately, returning errors, and I can't see why. Its saying that QueryForecastDecode is undefined, that there is an undefined offset, and that I'm trying to get the property of a non object (for the XML variables). That would be because of the undefined variable, I think. Any suggestions?

I really appreciate your help with this. I'm still relatively new to PHP. :)
Photo of mboltonwx

mboltonwx

  • 12 Posts
  • 0 Reply Likes
Played with it a bit.

Returning
<?php echo $ForecastArray; ?>
gets rid of the offset issue. Numbering the arrays caused for some reason.

Beneath the error messages that are left, it output the word "Array".
Photo of Globe199

Globe199

  • 14 Posts
  • 1 Reply Like
Sorry, you need this line underneath $f = 0 --

$QueryForecastDecode = json_decode($QueryForecast);

I missed that one when copying/pasting.
Photo of mboltonwx

mboltonwx

  • 12 Posts
  • 0 Reply Likes
Ok... that helped things along quite a bit. Now, only one error left, an undefined offset (4) on line 6.

Line 6 is:
while($QueryForecastDecode->{'forecast'}->{'simpleforecast'}->{'forecastday'}[$f])


The last line missing was obvious once you told me, but I don't have a clue here. I think it has something to do with $f.
Photo of Globe199

Globe199

  • 14 Posts
  • 1 Reply Like
$QueryForecastDecode = json_decode($QueryForecast);

// build forecast array // forecast array structure: Day, Hi, Lo, ForecastText, ForecastIconURL, PrecipPercent
$f = 0;
$ForecastArray = array();
while ($QueryForecastDecode->{'forecast'}->{'simpleforecast'}->{'forecastday'}[$f]) {
$Forecasts = $QueryForecastDecode->{'forecast'}->{'simpleforecast'}->{'forecastday'}[$f];
$ForecastArray[$f] = array(
$Forecasts->{'date'}->{'weekday_short'},
$Forecasts->{'high'}->{'fahrenheit'},
$Forecasts->{'low'}->{'fahrenheit'},
$Forecasts->{'conditions'},
$Forecasts->{'icon_url'},
$Forecasts->{'pop'}
);
$f++;
}

-------

Here's my code unformatted. The line wraps (what is this, a coding forum? :) might have been screwing with you.
Photo of Globe199

Globe199

  • 14 Posts
  • 1 Reply Like
Photo of mboltonwx

mboltonwx

  • 12 Posts
  • 0 Reply Likes
So close, but not working yet. Now I'm getting a "trying to get property of a non-object" error for forecast, simpleforecast, and forecastday.... I don't see why forecast, simpleforecast, and forecastday wouldn't be properties, since they're all layered in the XML.
Photo of Globe199

Globe199

  • 14 Posts
  • 1 Reply Like
paste your code to pastebin and I'll take a look.
Photo of mboltonwx

mboltonwx

  • 12 Posts
  • 0 Reply Likes
Photo of Globe199

Globe199

  • 14 Posts
  • 1 Reply Like
I don't see a line like this:

$QueryForecast = file_get_contents("http://api.wunderground.com/.../forec...;

You need the file_get_contents of the forecast URL before anything else.
Photo of mboltonwx

mboltonwx

  • 12 Posts
  • 0 Reply Likes
So I can't retrieve the user's forecast based on their IP address?

Also, that URL doesn't return anything. Neither does
http://api.wunderground.com/api/.../forecast/geolookup/conditions/q/zmw:zipcode.json
I think I'm missing something. I'm still getting the non-object messages.

Thanks for your patience and continued help.
Photo of Globe199

Globe199

  • 14 Posts
  • 1 Reply Like
Right, that was just a placeholder URL. You'll need your own key and to use a real URL as documented here:

http://www.wunderground.com/weather/a...

You can use the following URL for testing, but use your own key (replace {key} with your key):

http://pastebin.com/DqPDbvbZ
Photo of mboltonwx

mboltonwx

  • 12 Posts
  • 0 Reply Likes
Right. I've been using my key in the URL. But what I'm saying is that
http://api.wunderground.com/api/0e89f...
doesn't return anything. I've also tried using {key}/forecast/conditions/geolookup/q/zmw:zipcode.xml

And, geolookup/conditions/forecast/q/autoip.xml didn't work before when I was trying it with your PHP.

I'm just wondering why nothing is working when it comes to returning the forecast info. Geolookup/conditions/forecast works when I get current conditions.
Photo of Globe199

Globe199

  • 14 Posts
  • 1 Reply Like
Did you test your URL using the API request console?
Photo of mboltonwx

mboltonwx

  • 12 Posts
  • 0 Reply Likes
Yeah, it's ok. Can you only access forecast info for specific locations, and not automatically based on the user's location?
Photo of Globe199

Globe199

  • 14 Posts
  • 1 Reply Like
By user's location, do you mean geolocation (using location services)?

What I do is build a URL that queries the nearest airport (by ICAO).
Photo of mboltonwx

mboltonwx

  • 12 Posts
  • 0 Reply Likes
Right. I'm in Florida, but I want it so that somebody in New York or some other state can get their local forecast automatically, similar to the way autoip works with current conditions.