Page 1 of 1

nwsForecast: weather icons not loading

Posted: Mon Oct 06, 2025 6:35 pm
by danb35
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:
Screenshot 2025-10-06 at 14.33.08.png
Screenshot 2025-10-06 at 14.33.08.png (55.7 KiB) Viewed 12319 times
MT 19.0. Where should I be looking for this problem?

Re: nwsForecast: weather icons not loading

Posted: Wed Oct 08, 2025 6:09 am
by FSC830
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

Re: nwsForecast: weather icons not loading

Posted: Wed Oct 08, 2025 10:10 am
by danb35
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:

Code: Select all

							<img src="http://forecast.weather.gov/<?php echo $iconsSevenDay[1][$i]?>" class="nwsForecastIconImg">
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:

Code: Select all

<img src="http://forecast.weather.gov/" class="nwsForecastIconImg">
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.

Re: nwsForecast: weather icons not loading

Posted: Wed Oct 08, 2025 11:55 am
by danb35
ChatGPT came up with this as code to retrieve and parse the NWS API:

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']
    ];
}
It's going to take some work yet to integrate it into the rest of the block though.

Re: nwsForecast: weather icons not loading

Posted: Fri Oct 10, 2025 10:49 am
by danb35
Almost have it:
Screenshot 2025-10-10 at 06.48.33.png
Screenshot 2025-10-10 at 06.48.33.png (143.28 KiB) Viewed 12204 times
Just need to work a little more on parsing that valid date range.

Re: nwsForecast: weather icons not loading

Posted: Fri Oct 10, 2025 12:28 pm
by danb35
All right, done, at least as far as I can tell. The updated block is here:
https://github.com/danb35/MTBlocks/tree ... wsForecast

Re: nwsForecast: weather icons not loading

Posted: Sat Oct 11, 2025 12:14 am
by Gus
Updated NWS forecast block working well.
Thank you