nwsForecast: weather icons not loading
-
danb35
- Observer

- Posts: 20
- Joined: Sat Jan 20, 2018 4:14 pm
- Station model: Davis Pro 2
- Software: WeeWX
nwsForecast: weather icons not loading
I'm not sure if this is a common cause with my other thread, so I've made a new one. The nwsForecast block is loading the forecast data OK, but the images representing the forecast aren't:
MT 19.0. Where should I be looking for this problem?
-
FSC830
- Forecaster

- Posts: 195
- Joined: Thu Aug 02, 2018 11:40 am
- Station model: Davis Vantage Pro2
- Software: Meteobridge
Re: nwsForecast: weather icons not loading
Hi,
I dont use this block, but it looks like the path to the icons is not correct.
Or it is may be an issue at browser side.
Oh, just seen in your other post that you did a new install, so may be here are some PHP8 issues possible to.
Regards
I dont use this block, but it looks like the path to the icons is not correct.
Or it is may be an issue at browser side.
Oh, just seen in your other post that you did a new install, so may be here are some PHP8 issues possible to.
Regards
-
danb35
- Observer

- Posts: 20
- Joined: Sat Jan 20, 2018 4:14 pm
- Station model: Davis Pro 2
- Software: WeeWX
Re: nwsForecast: weather icons not loading
Well, I'm pretty sure this is at least part of the problem--it looks as though the icons are loaded via http. From line 179 of nwsForecastBlock.php:
Since I serve my site via HTTPS, this is likely to be a problem. Maybe not the whole problem, but almost certainly a problem.
But that doesn't seem to be everything; the "echo $iconsSevenDay" part doesn't seem to be returning anything. Here's what's in the HTML:
Edit: Well, the block's change log shows the last update in early 2018, so that may be the rest of the problem.
Edit 2: The current block operates by scraping HTML downloaded from https://forecast.weather.gov. I don't know if it was available in early 2018 when the block was last updated, but NWS now has an API:
https://www.weather.gov/documentation/services-web-api
A query to https://api.weather.gov/points/(lat),(long) will return a JSON object including (among other things) a URI for the 7-day forecast; a query to that URI will return the entire forecast, again as a JSON object. All the data currently displayed by the block is there, including HTTPS URLs to the icons. Seems it ought to be pretty straightforward to use this for the block rather than the HTML scrape, but that's easy for me to say.
Code: Select all
<img src="http://forecast.weather.gov/<?php echo $iconsSevenDay[1][$i]?>" class="nwsForecastIconImg">
But that doesn't seem to be everything; the "echo $iconsSevenDay" part doesn't seem to be returning anything. Here's what's in the HTML:
Code: Select all
<img src="http://forecast.weather.gov/" class="nwsForecastIconImg">Edit 2: The current block operates by scraping HTML downloaded from https://forecast.weather.gov. I don't know if it was available in early 2018 when the block was last updated, but NWS now has an API:
https://www.weather.gov/documentation/services-web-api
A query to https://api.weather.gov/points/(lat),(long) will return a JSON object including (among other things) a URI for the 7-day forecast; a query to that URI will return the entire forecast, again as a JSON object. All the data currently displayed by the block is there, including HTTPS URLs to the icons. Seems it ought to be pretty straightforward to use this for the block rather than the HTML scrape, but that's easy for me to say.
-
danb35
- Observer

- Posts: 20
- Joined: Sat Jan 20, 2018 4:14 pm
- Station model: Davis Pro 2
- Software: WeeWX
Re: nwsForecast: weather icons not loading
ChatGPT came up with this as code to retrieve and parse the NWS API:
It's going to take some work yet to integrate it into the rest of the block though.
Code: Select all
// Fetch grid and forecast URL from NWS API
$pointsUrl = "https://api.weather.gov/points/{$lat},{$lon}";
$ch = curl_init($pointsUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "MyWeatherApp (youremail@example.com)"); // required by NWS
$pointsResponse = curl_exec($ch);
curl_close($ch);
if ($pointsResponse === false) {
die("Error retrieving point data");
}
$pointsData = json_decode($pointsResponse, true);
if (!isset($pointsData['properties']['forecast'])) {
die("No forecast URL found for this location");
}
$forecastUrl = $pointsData['properties']['forecast'];
// Now fetch the forecast itself
$ch = curl_init($forecastUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "MyWeatherApp (youremail@example.com)");
$forecastResponse = curl_exec($ch);
curl_close($ch);
if ($forecastResponse === false) {
die("Error retrieving forecast data");
}
$forecastData = json_decode($forecastResponse, true);
if (!isset($forecastData['properties']['periods'])) {
die("No forecast periods found");
}
$periods = $forecastData['properties']['periods'];
$data = [];
// Build the $data array to match your existing structure
foreach ($periods as $period) {
$data[] = [
'time' => $period['name'],
'temp' => $period['temperature'] . ' ' . $period['temperatureUnit'],
'forecast' => $period['shortForecast'],
'wind' => $period['windDirection'] . ' ' . $period['windSpeed'],
'detailed' => $period['detailedForecast']
];
}
-
danb35
- Observer

- Posts: 20
- Joined: Sat Jan 20, 2018 4:14 pm
- Station model: Davis Pro 2
- Software: WeeWX
Re: nwsForecast: weather icons not loading
Almost have it:
Just need to work a little more on parsing that valid date range.
-
danb35
- Observer

- Posts: 20
- Joined: Sat Jan 20, 2018 4:14 pm
- Station model: Davis Pro 2
- Software: WeeWX
Re: nwsForecast: weather icons not loading
All right, done, at least as far as I can tell. The updated block is here:
https://github.com/danb35/MTBlocks/tree ... wsForecast
https://github.com/danb35/MTBlocks/tree ... wsForecast
- Gus
- Observer

- Posts: 10
- Joined: Sun Aug 20, 2017 11:17 pm
- Location: Halethorpe, Maryland
- Station model: Davis Vantage Pro 2+
- Software: Weather Display
- Contact:
Re: nwsForecast: weather icons not loading
Updated NWS forecast block working well.
Thank you
Thank you