You can try this version of TweetWX-forecast.php <?php
/* TweetWX-forecast1.php
This package is based on code by Joe Chung at http://nullinfo.wordpress.com/oauth-twitter/
Repackaged for TweetWX by Ken True - Saratoga-weather.org 08-Jun-2010
Docs at http://saratoga-weather.org/scripts-TweetWX.php
TweetWX-forecast.php V1.00 - 11-Jun-2010 - Initial relase
// Version 2.00 - 17-Jul-2015 - major changes for new NWS dual-icon forecast
This utility function should be used once to get the OAuth request_token and request_token_secret
for use in the next step of setup.
*/
$Version = 'TweetWX-forecast.php - V2.00 - 17-Jul-2015';
header("Content-type: text/plain\n");
if (phpversion() < 5) {
echo 'Failure: This Script requires PHP version 5 or greater. You only have PHP version: ' . phpversion();
exit;
}
// --------------- settings ----------------------
$tweetWXdir = './';
$wxdir = '../'; // path to the advforecast2.php and Settings.php (if used in template)
$tweetHeader = 'NWS Fcst: '; # leave the : in the text.. may be replaced by (n/3): if multi-tweet is used
$tweetTags = 'http://bit.ly/NKksn #CAwx'; // customize for your Twitter tags and/or website url (shortened)
// Fill in the next 2 variables from the values produced in TweetWX-setup2.php.
$access_token = '-replace-with-token-from-TweetWX-setup2-'; // oauth_token
$access_token_secret = '-replace-with-token_secret-from-TweetWX-setup2-'; // oauth_token_secret
// ----------- end settings ----------------------
require_once($tweetWXdir . 'TweetWX-globals.php');
if (file_exists($wxdir . "Settings.php")) {
include_once($wxdir . "Settings.php");
}
print "$Version\n";
$doPrintNWS = false;
$_REQUEST['force'] = '1';
include_once($wxdir. 'advforecast2.php');
print "$Version\n";
// Extract the 'good bits' from the forecasticons array for the text-only forecast
//
/*
<-- part 0 = 'Tonight' -->
<-- part 1 = '' -->
<-- part 2 = '<img src="./forecast/images/nfew.jpg" alt="Tonight: Patchy fog after 2am. Otherwise, increasing clouds, with a low around 61. Calm wind becoming northeast around 5 mph. " title="Tonight: Patchy fog after 2am. Otherwise, increasing clouds, with a low around 61. Calm wind becoming northeast around 5 mph. " class="forecast-icon"/>' -->
<-- part 3 = 'Mostly Clear' -->
<-- part 4 = 'then Patchy' -->
<-- part 5 = 'Fog' -->
*/
# process the raw forecast icons for the forecast text to use
for ($i=0;$i<count($forecasticons);$i++) {
$fparts = explode('<br/>',$forecasticons[$i]);
// print "<-- icon $i = '".$forecasticons[$i]." -->\n";
// foreach ($fparts as $n => $t) {print "<-- part $n = '" . $t . "' -->\n"; }
$t = $fparts[0];
$t .= (trim($fparts[1])<>'')?' '.trim($fparts[1]):'';
$t .= ': '. $fparts[3];
$t .= (trim($fparts[4])<>'')?' '.trim($fparts[4]):'';
$t .= (isset($fparts[5]) and trim($fparts[5])<>'')?' '.trim($fparts[5]):'';
$t .= (isset($fparts[6]) and trim($fparts[6])<>'')?' '.trim($fparts[6]):'';
$t .= (isset($fparts[7]) and trim($fparts[7])<>'')?' '.trim($fparts[7]):'';
$t .= (isset($fparts[8]) and trim($fparts[8])<>'')?' '.trim($fparts[8]):'';
$t .= (isset($fparts[9]) and trim($fparts[9])<>'')?' '.trim($fparts[9]):'';
$t .= (isset($fparts[10]) and trim($fparts[10])<>'')?' '.trim($fparts[10]):'';
$t .= ', '.$forecasttemp[$i] . '; ';
$t = preg_replace('|\s\s+|is',' ',$t);
# $t = preg_replace('| °|is','°',$t);
$t = preg_replace('| °|is','',$t);
$fcst[$i] = strip_tags($t);
}
// print_r($fcst);
# try full length message first
$message = $tweetHeader . ' '. $fcst[0] . $fcst[1] . $fcst[2] . ' ' . $tweetTags;
if (strlen($message) > 135) {
print $message . "\nMessage is too long (".strlen($message). " bytes) -- reducing size.\n";
$message = TW_shorten_tweet($message);
}
echo $message."\nlen=".strlen($message)." bytes.\n";
if (strlen($message) > 135) {
# use 3 messages instead
print "\nMessage still too long (".strlen($message)." bytes). Formatting 3 tweets instead\n";
for ($i=0;$i<3;$i++) {
$n = $i+1;
$t = str_replace(":","($n/3):",$tweetHeader);
$message = $t . $fcst[$i] . ' ' . $tweetTags;
$message = preg_replace('|\s\s+|is',' ',$message); # every char counts :)
if(strlen($message)> 135) {
print $message . "\nMessage is too long (".strlen($message). " bytes) -- reducing size.\n";
$message = TW_shorten_tweet($message);
}
$utfmessage = iconv('ISO-8859-1','UTF-8//TRANSLIT',$message);
echo "\nUTF='$utfmessage' len=".strlen($utfmessage)." bytes.\nstatus=";
// POST a tweet using OAuth authentication
$retarr = post_tweet(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET,
$utfmessage, $access_token, $access_token_secret,
true, true);
sleep(2); # Pause between tweets
print "\n";
}
} else {
$utfmessage = iconv('ISO-8859-1','UTF-8//TRANSLIT',$message);
echo "UTF='$utfmessage' len=".strlen($utfmessage)." bytes.\nstatus=";
// POST a tweet using OAuth authentication
$retarr = post_tweet(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET,
$utfmessage, $access_token, $access_token_secret,
true, true);
}
function TW_shorten_tweet($M) {
# -- shorten the tweet to fit the 140 character limit (if possible)
$msg = $M;
# list of abbreviations to apply to shorten the tweet
# Note.. these can be regular expressions in the first field for the preg_replace()
$ALIST = '
Sunday|Sun|
Monday|Mon|
Tuesday|Tue|
Wednesday|Wed|
Thursday|Thu|
Friday|Fri|
Saturday|Sat|
Afternoon|Aft.noon|
chance|Chc|
Partly|Ptly|
Heavy|Hvy|
Rain/Snow|RaSn|
Breezy|Wnd|
Snow|Sn|
Frost|Frst|
Blustery|Wnd|
Drizzle|Driz|
Areas Dense Fog|Area Dns Fog|
likely|lkly|
slight|Slt|
Windy|Wndy|
T-storms|Tstrm|
Tropical Storm|TropStrm|
Conditions|Cond|
Expected|Expctd|
Isolated|Isol|
Possible|Poss|
Severe|Svr|
Patchy|Ptchy|
showers|Shwrs|
Mostly|Mstly|
Clear|Clr|
Cloudy|Cldy|
Clouds|Clds|
Scattered|Sctd|
increasing|Incr|
decreasing|Decr|
\sand\s|+|
\s*then\s*|->|
\.\s+|+|
';
$A = explode("\n",$ALIST);
# Apply each shortner to the text
foreach ($A as $rec) {
list($k,$v) = explode('|',$rec.'||');
if (strlen($v) < 1) {continue;}
$msg = preg_replace("|$k|is",$v,$msg);
// print "<!-- repl '$k' with '$v' -->\n";
}
$msg = preg_replace('|\s\s+|is',' ',$msg); # remove extra spaces if any
return ($msg);
} // end TW_shorten_tweet function
?>
It's what I'm currently using on my site. It will split big forecasts into three separate tweets, one for each period, if need be.