cloudy cloudy

Author Topic: NWS Weather Forecast Messenger PHP Script  (Read 20411 times)

0 Members and 2 Guests are viewing this topic.

Offline MCHALLIS

  • Posts: 2,149
  • Long Beach, WA USA
    • Weather for Long Beach, WA USA
NWS Weather Forecast Messenger PHP Script
« on: July 10, 2009, 09:13:27 AM »
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

4) 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".

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

6) 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
« Last Edit: September 30, 2011, 12:56:38 AM by MCHALLIS »

Offline MCHALLIS

  • Posts: 2,149
  • Long Beach, WA USA
    • Weather for Long Beach, WA USA
Re: NWS Weather Forecast Messenger PHP Script
« Reply #1 on: July 10, 2009, 09:35:49 AM »
Version: 1.01 - 10-Jul-2009 - fixed flood filter (do not use version 1.00)

Offline Budgie

  • Martin
  • Global Moderator
  • Posts: 5,977
  • CWOP: CW7959
  • Lochaber, Scotland
    • Lochaber-Weather
Re: NWS Weather Forecast Messenger PHP Script
« Reply #2 on: July 10, 2009, 09:48:07 AM »
Live demo URL needs a "/weather" removed Mike.  :wink:

Offline MCHALLIS

  • Posts: 2,149
  • Long Beach, WA USA
    • Weather for Long Beach, WA USA
Re: NWS Weather Forecast Messenger PHP Script
« Reply #3 on: July 11, 2009, 04:51:01 AM »
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
« Last Edit: July 23, 2009, 01:57:04 AM by MCHALLIS »

Offline MCHALLIS

  • Posts: 2,149
  • Long Beach, WA USA
    • Weather for Long Beach, WA USA
Re: NWS Weather Forecast Messenger PHP Script
« Reply #4 on: July 22, 2009, 10:31:28 PM »
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';   
« Last Edit: July 23, 2009, 02:09:01 AM by MCHALLIS »

Offline MCHALLIS

  • Posts: 2,149
  • Long Beach, WA USA
    • Weather for Long Beach, WA USA
Re: NWS Weather Forecast Messenger PHP Script
« Reply #5 on: July 31, 2009, 05:35:16 PM »
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

Offline MCHALLIS

  • Posts: 2,149
  • Long Beach, WA USA
    • Weather for Long Beach, WA USA
Re: NWS Weather Forecast Messenger PHP Script
« Reply #6 on: August 16, 2009, 06:37:48 AM »
Version: 1.06 - 15-Aug-2009
- added email Bcc feature
- improved instructions inside readme.txt

Offline fleegs79

  • Posts: 6
    • Harvey, MI Weather
Re: NWS Weather Forecast Messenger PHP Script
« Reply #7 on: November 18, 2009, 03:07:43 AM »
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.


Offline fleegs79

  • Posts: 6
    • Harvey, MI Weather
Re: NWS Weather Forecast Messenger PHP Script
« Reply #8 on: January 17, 2010, 03:56:27 PM »
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:
Code: [Select]
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 '<br />';

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

} // 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.

Offline MCHALLIS

  • Posts: 2,149
  • Long Beach, WA USA
    • Weather for Long Beach, WA USA
Re: NWS Weather Forecast Messenger PHP Script
« Reply #9 on: June 14, 2010, 06:19:43 AM »
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
« Last Edit: June 14, 2010, 06:22:37 AM by MCHALLIS »

Offline MCHALLIS

  • Posts: 2,149
  • Long Beach, WA USA
    • Weather for Long Beach, WA USA
Re: NWS Weather Forecast Messenger PHP Script
« Reply #10 on: September 30, 2011, 12:57:32 AM »
Version: 2.01 - 29-Sep-2011 - NWS changed the data URL, this update uses the correct URL.

Download URL on page 1

Offline DavenLC

  • Posts: 11
  • LC, TX
  • OS/Browser:
  • Win NT 10.0
  • Firefox 51.0
    • Community Web site
Re: NWS Weather Forecast Messenger PHP Script
« Reply #11 on: February 17, 2017, 12:24:27 PM »
Messenger quit working...   Is there an update?

Offline ALITTLEweird1

  • Mark
  • Global Moderator
  • Posts: 5,083
  • North Bend, WA
  • OS/Browser:
  • Android 6.0.1
  • Chrome 51.0.2704.106
    • North Bend Weather
Re: NWS Weather Forecast Messenger PHP Script
« Reply #12 on: February 17, 2017, 02:14:10 PM »
It could be because the nws changed their url from http to https.
"Nature can do without man, but man cannot do without nature."

Davis VP2 + VP2 Solar + Logitech Webcam

Offline DavenLC

  • Posts: 11
  • LC, TX
  • OS/Browser:
  • Win NT 10.0
  • Firefox 51.0
    • Community Web site
Re: NWS Weather Forecast Messenger PHP Script
« Reply #13 on: February 17, 2017, 02:32:53 PM »
Code: [Select]
$RSS_URL = 'http://graphical.weather.gov/xml/sample_products/browser_interface/ndfdBrowserClientByDay.php?&lat='.
or

Code: [Select]
$RSS_URL = 'httpS://graphical.weather.gov/xml/sample_products/browser_interface/ndfdBrowserClientByDay.php?&lat='.
still not working...    hmmnm

Offline Gus

  • Posts: 620
  • Halethorpe, Maryland USA
  • OS/Browser:
  • Win NT 10.0
  • Chrome 56.0.2924.87
    • Halethorpe Weather-Watch
Re: NWS Weather Forecast Messenger PHP Script
« Reply #14 on: February 17, 2017, 02:38:16 PM »
Change httpS to https in $RSS_URL = 'http://httpS://graphical.
Davis VP2+ & LabJack w/lightning counter, soil/grass temp
AMD AthlonIIx2 220, 4GB, Windows7 CW1550 KMDBALTI12

 

cumulus