NWS Weather Forecast Messenger PHP Script

NWS Weather Forecast Messenger PHP Script by Mike Challis

Version: 1.00 - 10-Jul-2009 - release

Version: 1.01 - 10-Jul-2009 - fixed flood filter (do not use version 1.00)

Version: 1.02 - 10-Jul-2009 - added features: selectable temp units setting.

  • if twitter message is too long, attempts to shorten it by abbreviating some weather condition words.
  • prints on the web page how long the flood filter is still in effect for…
    (Flood filter is in effect for 5 hours, 9 minutes, no twitter was sent)

Version: 1.04 - 22-Jul-2009 - added post authentication feature.

Version: 1.06 - 15-Aug-2009

  • added email Bcc feature
  • improved instructions inside readme.txt

Version: 1.07 - 16-Nov-2009 - add Return-Path header to email.

Version: 2.00 - 13-Jun-2010 - added OAuth/Twitter authentication (TweetWX642) because user/password authentication is being disallowed after June 30th, 2010 (Thanks to Joe Chung and Ken True for providing the auth code)

Version: 2.01 - 29-Sep-2011 - NWS changed the data URL, this update uses the correct URL.

Partially based on a variation of the following scripts:
NOAA Top Weather Advisory Alerter PHP Script by Mike Challis
http://www.642weather.com/weather/scripts-noaa-top-weather-alerter.php
RSS Top Warning PHP Script by Ken True http://saratoga-weather.org/scripts.php
PHP NOAA Advisory Text by Tom Chaplin http://www.carterlake.org/weatherphp.php
Twitter Code by http://morethanseven.net/2007/01/20/posting-to-twitter-using-php/
Forecast code based on http://jivebay.com/2008/04/08/free-weather-forecast/

What does it do?
This PHP script is used to check the NOAA Weather XML Forecast feeds on a timed schedule,
then send a forecast message.
Uses the forecast for the the latitude longitude you have set in the settings.
The forecast message is short enough to send to twitter, a cell phone, blackberry, or email.
Supports sending messages by Email and Twitter, each method can be enabled or disabled.
Messages can be customized in the script settings.

Includes some of the features of the NOAA Top Weather Advisory Alerter PHP Script
http://www.642weather.com/weather/scripts-noaa-top-weather-alerter.php

What do I need?
PHP 5 with CURL support is required
… Web server hosting account with PHP support and ability to send emails from PHP
… Ability to schedule a cron job or a scheduler program on your PC that can request a URL every 6 hours.
… your PHP server MUST allow PHP scripts permission to write files.
… PHP safe_mode is recommended to be off

How to use it:

  1. Set your settings in the “settings” of the PHP script.
    Be sure to set the latitude, longitude, twitter user password and your email address.
    Also set the settings for Email and Twitter to customize the messages, each method can be enabled or disabled.

  2. Upload this php file to your web server

  3. Test from a URL, see if you can see the HTML output
    Example: http://www.yoursite.com/nws-forecast-messenger.php
    After testing move it to a private non-public web server folder.
    Warning: it is recommended that you put the PHP file in a non-public www directory!
    because you do not want people clicking the URL and flooding you with emails and twitters!!
    If you put it in a non-public www directory, then the only way you can call it is from a cron job
    There is a flood filter setting that will prevent the flood problem, but make you should test it,
    don’t just “set it and forget it”.

  4. It is recommended that you test an email or twitter to make sure you will receive one.

  5. Schedule automatic timed updates (see readme.txt)

Download
www.642weather.com/weather/scripts/nws-forecast-messenger.zip

Live Demo
www.642weather.com/weather/nws-forecast-messenger.php

My Twitter
http://twitter.com/642weather

Version: 1.01 - 10-Jul-2009 - fixed flood filter (do not use version 1.00)

Live demo URL needs a “/weather” removed Mike. :wink:

Version: 1.02 - 10-Jul-2009 - added features: selectable temp units setting.

  • if twitter message is too long, attempts to shorten it by abbreviating some weather condition words.
  • prints on the web page how long the flood filter is still in effect for…
    (Flood filter is in effect for 5 hours, 9 minutes, no twitter was sent)

Version: 1.03 - 14-Jul-2009 - fixed possible fatal error regarding attributes

Version: 1.04 - 22-Jul-2009 - added post authentication feature.
Now you can edit your cron job to use CURL and pass a username and password
so that the script can be in public view and it only emails when called from your cron job with the correct username and password.
This is the best way to allow the script to be in public and have the most accurate cron times, such as I use: every 6 hours (3,9,16,22) 3pm,9pm,3am,9am
This is a sample of my cron: (be sure to set your_username and your_password)
curl -d ‘username=your_username&password=your_password’ http://www.642weather.com/weather/nws-forecast-messenger.php >/dev/null 2>&1

Version: 1.05 - 22-Jul-2009

  • added configurable array of shorten_message words to settings.

  • shorten_twitter function changed to a shorten_message function that can shorten emails and twitters

  • added two more settings:
    // number of characters to attempt to shorten email message to
    $email_shorten = ‘120’;

// number of characters to attempt to shorten twitter message to
$twitter_shorten = ‘140’;

I figured out how to send my tweets to Facebook. This is how:
Log into Facebook and go to this app:
http://www.facebook.com/selectivetwitter
Click settings and add your Twitter username, then grant permissions.
End a tweet with #fb when you want it to update your Facebook status - simple.

I made a Facebook page for 642weather, then made a personal profile for myself, I am the admin of the page now.
Pages and profiles are two different things.

http://www.facebook.com/pages/Long-Beach-Weather-Long-Beach-Washington-98631-USA/102474670891?ref=nf

Version: 1.06 - 15-Aug-2009

  • added email Bcc feature
  • improved instructions inside readme.txt

Has anybody modified the script to only look at 12 hour periods? I’d like to show a little more detail, instead of having the worst weather for the 24hr period. For example, my forecast for tomorrow shows rain likely even though it will be sunny all day and the best chance for rain is late tomorrow night.

Thanks.

If anybody is interested in using the new GeoTag API with twitter for your Forecast Tweets, modify the following function towards the bottom of the script:

function twitter_alert($twitter_message) {
 global $twitter_user, $twitter_password, $twitter_url, $user_agent, $latitude, $longitude;

  // Set up and execute the curl process
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
  curl_setopt($ch, CURLOPT_URL, "$twitter_url");
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, "status=$twitter_message&lat=$latitude&long=$longitude");
  curl_setopt($ch, CURLOPT_USERPWD, "$twitter_user:$twitter_password");
  $buffer = curl_exec($ch);
  curl_close($ch);

  echo '
';

  // check for success or failure for twitter message sending
  if (empty($buffer)) {
          echo 'WARNING: Twitter message not sent (check twitter settings)
';
  } else {
          echo 'A Twitter was sent:
';
          echo $twitter_message.'
';
          echo 'Twitter message length: '.strlen($twitter_message).'
';
  }

} // end function twitter_alert

The only changes were to the global variables and the CURLOPT_POSTFIELDS lines. MCHALLIS, thanks for a great script. I find it very useful.

Version: 2.00 - 13-Jun-2010 - added OAuth/Twitter authentication (TweetWX642) because user/password authentication is being disallowed after June 30th, 2010 (Thanks to Joe Chung and Ken True for providing the auth code)

Twitter usage:
See readme.txt inside TweetWX642 folder

Version: 2.01 - 29-Sep-2011 - NWS changed the data URL, this update uses the correct URL.

Download URL on page 1

Messenger quit working… Is there an update?

It could be because the nws changed their url from http to https.

$RSS_URL = 'http://graphical.weather.gov/xml/sample_products/browser_interface/ndfdBrowserClientByDay.php?&lat='.

or

$RSS_URL = 'httpS://graphical.weather.gov/xml/sample_products/browser_interface/ndfdBrowserClientByDay.php?&lat='.

still not working… hmmnm

Change httpS to https in $RSS_URL = 'httpS://graphical.

lol… it is small letters… I just made big ‘S’ to represent the code change

I had the same problem. Change your cache time to 5 or something

// Cache time xx seconds
$refetchSeconds = 5;

Make sure flood filter is 0
$flood_filter_enabled = 0;

Hit the script again and should be ok. Worked for me.
Then change your cache and flood filter back once again.

done… still same issue. Deleted cache file too.

Forecast Information Unavailable, error fetching or reading data from the NOAA server.

I have version

 */
$Version = '2.02 - 16-Jun-2013';
/*

and I didnt see an updated one

I’m a big doofus… I didnt have a CURL CERT. I do now. it’s back to working