Weather on Your PHP-based Website

If you live in an area that receives both days of freezing sub-zero temperatures and days featuring sweltering heat indexes over the course of a year (like Minnesota), it is important to know what the weather is like before you step out the door in the morning. Because weather is an important factor when people plan their day, and because it helps paint a picture of the experience in a specific place to people hundreds or thousands of miles away, I sometimes like to include current weather conditions on websites I develop.

To put weather on your website, you have to grab weather data from some source. Unfortunately, this is easier said than done. I have spent a fair amount of time looking for a lightweight script that grabs accurate weather data and works with PHP-based websites.

I used this add-on for SimplePie to grab Yahoo! Weather for a long time. SimplePie is an awesome RSS parser, that grabs RSS feeds, caches them, and allows you to include them on your website. It worked well, but the add-on pulled a lot of extra weather information that I didn’t need, such as sunrise and sunset. In most cases, I like to provide just a bit of weather data in the corner of a page, and give people a link if they want to see more. In addition, Yahoo! gets it’s data from The Weather Channel, so there could be a bit of a time lag before the data is updated. I really wanted to use weather data from the National Weather Service, who conducts official measurements all over the Unites States each hour.

A (not current) example of weather you could put on your website using my script.

I recently found a script that the University of Central Florida developed to pull NOAA weather data on GitHub. It’s lightweight, has it’s own built-in cache, and requires no other scripts to operate. I liked the plugin, but wanted to make some changes. The script was open source, so I made a number of programmatic changes and added to the documentation.

See the script, Current NOAA Weather Grabber, on GitHub.

If you are looking to include current weather conditions on your PHP-based website, give it a try. I think you’ll enjoy how easy to use, lightweight, and accurate this weather script is.

Check it out, and let me know if you have any questions, comments, or suggestions.

2 Responses

Leave a Reply