Return to Snippet

Revision: 5030
at March 6, 2008 08:09 by johnself


Updated Code
<?php 
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, 'http://www.weather.gov/alerts/wwarssget.php?zone=TNZ027');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$contents = curl_exec($ch);
curl_close($ch);

if(!empty($contents)) {
	
	$db = new SQLiteDatabase('noaa.db2');
	/*
	$time = time();
	$db->query("BEGIN;
	CREATE TABLE status ( id INTEGER PRIMARY KEY, timestamp );
	INSERT INTO status (timestamp) VALUES($time);
	COMMIT;"); 
	*/
	$result = $db->query('SELECT * FROM status WHERE id = 1 LIMIT 1',SQLITE_ASSOC); //SQLITE_NUM SQLITE_BOTH (Default)
	$data = $result->current();
	//echo $data['timestamp'];
	
	$xml = new SimpleXMLElement($contents);
	foreach($xml->channel->item as $item) {
	
			if (strstr(trim($item->title),'Tornado Warning')) {
					
				if($data['timestamp'] < (time()-1800)) { // Set time in seconds between alerts
					
				$db->query("UPDATE status SET timestamp = ".time()." WHERE id = 1");

				require("phpmailer/class.phpmailer.php"); //http://sourceforge.net/projects/phpmailer
				$mail = new PHPMailer();
				$mail->IsSMTP(); // set mailer to use SMTP
				$mail->SMTPAuth = true;     // turn on SMTP authentication
				$mail->Username = "xxxx";  // SMTP username
				$mail->Password = "xxxx"; // SMTP password
				$mail->SetLanguage("en",dirname(__FILE__) . "/phpmailer/language/"); 
				$mail->From = "[email protected]";
				$mail->FromName = "John Self";
				$mail->AddAddress("[email protected]");
				//$mail->AddAddress("[email protected]"); // Add as many recipients as you want
				$mail->IsHTML(false);
				$mail->Subject = "WEATHER ALERT";
				$mail->Body    = "A TORNADO WARNING HAS BEEN ISSUED FOR DAVIDSON COUNTY";
				$mail->AltBody = "A TORNADO WARNING HAS BEEN ISSUED FOR DAVIDSON COUNTY";
		
				if(!$mail->Send())
				{
				   echo "Message could not be sent.
";
				   echo "Mailer Error: " . $mail->ErrorInfo;
				   exit;
				}
				exit;
			}		
		}
	}
}
?>

Revision: 5029
at February 6, 2008 10:17 by johnself


Updated Code
<?php 
session_start();
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, 'http://www.weather.gov/alerts/wwarssget.php?zone=TNZ027');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$contents = curl_exec($ch);
curl_close($ch);

if(!empty($contents)) {
	
	$db = new SQLiteDatabase('noaa.db2');
	/*
	$time = time();
	$db->query("BEGIN;
	CREATE TABLE status ( id INTEGER PRIMARY KEY, timestamp );
	INSERT INTO status (timestamp) VALUES($time);
	COMMIT;"); 
	*/
	$result = $db->query('SELECT * FROM status WHERE id = 1 LIMIT 1',SQLITE_ASSOC); //SQLITE_NUM SQLITE_BOTH (Default)
	$data = $result->current();
	//echo $data['timestamp'];
	
	$xml = new SimpleXMLElement($contents);
	foreach($xml->channel->item as $item) {
	
			if (strstr(trim($item->title),'Tornado Warning')) {
					
				if($data['timestamp'] < (time()-1800)) { // Set time in second between alerts
					
				$db->query("UPDATE status SET timestamp = ".time()." WHERE id = 1");

				require("phpmailer/class.phpmailer.php"); //http://sourceforge.net/projects/phpmailer
				$mail = new PHPMailer();
				$mail->IsSMTP(); // set mailer to use SMTP
				$mail->SMTPAuth = true;     // turn on SMTP authentication
				$mail->Username = "xxxx";  // SMTP username
				$mail->Password = "xxxx"; // SMTP password
				$mail->SetLanguage("en",dirname(__FILE__) . "/phpmailer/language/"); 
				$mail->From = "[email protected]";
				$mail->FromName = "John Self";
				$mail->AddAddress("[email protected]");
				//$mail->AddAddress("[email protected]"); // Add as many recipients as you want
				$mail->IsHTML(false);
				$mail->Subject = "WEATHER ALERT";
				$mail->Body    = "A TORNADO WARNING HAS BEEN ISSUED FOR DAVIDSON COUNTY";
				$mail->AltBody = "A TORNADO WARNING HAS BEEN ISSUED FOR DAVIDSON COUNTY";
		
				if(!$mail->Send())
				{
				   echo "Message could not be sent.
";
				   echo "Mailer Error: " . $mail->ErrorInfo;
				   exit;
				}
				exit;
			}		
		}
	}
}
?>

Revision: 5028
at February 6, 2008 10:09 by johnself


Updated Code
<?php 
session_start();
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, 'http://www.weather.gov/alerts/wwarssget.php?zone=TNZ027');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$contents = curl_exec($ch);
curl_close($ch);

if(!empty($contents)) {
	
	$db = new SQLiteDatabase('noaa.db2');
	/*
	$db->query("BEGIN;
	CREATE TABLE status ( id INTEGER PRIMARY KEY, timestamp );
	INSERT INTO status (timestamp) VALUES($time);
	COMMIT;"); 
	*/
	$result = $db->query('SELECT * FROM status WHERE id = 1 LIMIT 1',SQLITE_ASSOC); //SQLITE_NUM SQLITE_BOTH (Default)
	$data = $result->current();
	//echo $data['timestamp'];
	
	$xml = new SimpleXMLElement($contents);
	foreach($xml->channel->item as $item) {
	
			if (strstr(trim($item->title),'Tornado Warning')) {
					
				if($data['timestamp'] < (time()-1800)) { // Set time in second between alerts
					
				$db->query("UPDATE status SET timestamp = ".time()." WHERE id = 1");

				require("phpmailer/class.phpmailer.php"); //http://sourceforge.net/projects/phpmailer
				$mail = new PHPMailer();
				$mail->IsSMTP(); // set mailer to use SMTP
				$mail->SMTPAuth = true;     // turn on SMTP authentication
				$mail->Username = "xxxx";  // SMTP username
				$mail->Password = "xxxx"; // SMTP password
				$mail->SetLanguage("en",dirname(__FILE__) . "/phpmailer/language/"); 
				$mail->From = "[email protected]";
				$mail->FromName = "John Self";
				$mail->AddAddress("[email protected]");
				//$mail->AddAddress("[email protected]"); // Add as many recipients as you want
				$mail->IsHTML(false);
				$mail->Subject = "WEATHER ALERT";
				$mail->Body    = "A TORNADO WARNING HAS BEEN ISSUED FOR DAVIDSON COUNTY";
				$mail->AltBody = "A TORNADO WARNING HAS BEEN ISSUED FOR DAVIDSON COUNTY";
		
				if(!$mail->Send())
				{
				   echo "Message could not be sent.
";
				   echo "Mailer Error: " . $mail->ErrorInfo;
				   exit;
				}
				exit;
			}		
		}
	}
}
?>

Revision: 5027
at February 6, 2008 09:34 by johnself


Updated Code
<?php 
session_start();
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, 'http://www.weather.gov/alerts/wwarssget.php?zone=TNZ027');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$contents = curl_exec($ch);
curl_close($ch);

if(!empty($contents)) {
	
	$db = new SQLiteDatabase('noaa.db2');
	/*
	$db->query("BEGIN;
	CREATE TABLE status ( id INTEGER PRIMARY KEY, timestamp );
	INSERT INTO status (timestamp) VALUES($time);
	COMMIT;"); 
	*/
	$result = $db->query('SELECT * FROM status WHERE id = 1 LIMIT 1',SQLITE_ASSOC); //SQLITE_NUM SQLITE_BOTH (Default)
	$data = $result->current();
	//echo $data['timestamp'];
	
	$xml = new SimpleXMLElement($contents);
	foreach($xml->channel->item as $item) {
	
			if (strstr(trim($item->title),'Tornado Warning')) {
					
				if($data['timestamp'] < (time()-1800)) { // Set time in second between alerts
					
				$db->query("UPDATE status SET timestamp = ".time()." WHERE id = 1");

				require("phpmailer/class.phpmailer.php");
				$mail = new PHPMailer();
				$mail->IsSMTP(); // set mailer to use SMTP
				$mail->SMTPAuth = true;     // turn on SMTP authentication
				$mail->Username = "xxxx";  // SMTP username
				$mail->Password = "xxxx"; // SMTP password
				$mail->SetLanguage("en",dirname(__FILE__) . "/phpmailer/language/"); 
				$mail->From = "[email protected]";
				$mail->FromName = "John Self";
				$mail->AddAddress("[email protected]");
				//$mail->AddAddress("[email protected]"); // Add as many recipients as you want
				$mail->IsHTML(false);
				$mail->Subject = "WEATHER ALERT";
				$mail->Body    = "A TORNADO WARNING HAS BEEN ISSUED FOR DAVIDSON COUNTY";
				$mail->AltBody = "A TORNADO WARNING HAS BEEN ISSUED FOR DAVIDSON COUNTY";
		
				if(!$mail->Send())
				{
				   echo "Message could not be sent.
";
				   echo "Mailer Error: " . $mail->ErrorInfo;
				   exit;
				}
				exit;
			}		
		}
	}
}
?>

Revision: 5026
at February 6, 2008 09:33 by johnself


Initial Code
<?php 
session_start();
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, 'http://www.weather.gov/alerts/wwarssget.php?zone=TNZ027');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$contents = curl_exec($ch);
curl_close($ch);

if(!empty($contents)) {
	
	$db = new SQLiteDatabase('noaa.db2');
	/*
	$db->query("BEGIN;
	CREATE TABLE status ( id INTEGER PRIMARY KEY, timestamp );
	INSERT INTO status (timestamp) VALUES($time);
	COMMIT;"); 
	*/
	$result = $db->query('SELECT * FROM status WHERE id = 1 LIMIT 1',SQLITE_ASSOC); //SQLITE_NUM SQLITE_BOTH (Default)
	$data = $result->current();
	//echo $data['timestamp'];
	
	$xml = new SimpleXMLElement($contents);
	foreach($xml->channel->item as $item) {
	
			if (strstr(trim($item->title),'Tornado Warning')) {
					
				if($data['timestamp'] < (time()-1800)) { // Set time in second between alerts
					
				$db->query("UPDATE status SET timestamp = ".time()." WHERE id = 1");

				require("phpmailer/class.phpmailer.php");
				$mail = new PHPMailer();
				$mail->IsSMTP(); // set mailer to use SMTP
				$mail->SMTPAuth = true;     // turn on SMTP authentication
				$mail->Username = "xxxx";  // SMTP username
				$mail->Password = "xxxx"; // SMTP password
				$mail->SetLanguage("en",dirname(__FILE__) . "/phpmailer/language/"); 
				$mail->From = "[email protected]";
				$mail->FromName = "John Self";
				$mail->AddAddress("[email protected]");
				//$mail->AddAddress("[email protected]");
				$mail->IsHTML(false);
				$mail->Subject = "WEATHER ALERT";
				$mail->Body    = "A TORNADO WARNING HAS BEEN ISSUED FOR DAVIDSON COUNTY";
				$mail->AltBody = "A TORNADO WARNING HAS BEEN ISSUED FOR DAVIDSON COUNTY";
		
				if(!$mail->Send())
				{
				   echo "Message could not be sent.
";
				   echo "Mailer Error: " . $mail->ErrorInfo;
				   exit;
				}
				exit;
			}		
		}
	}
}
?>

Initial URL

                                

Initial Description
In light of last nights tornado warnings here in TN, I thought it appropriate to post this little bit of code that I use to alert myself and others of dangerous weather conditions via SMS/Email alerts.

I run this through cron at Dreamhost without issues.

Initial Title
SMS/Email Weather Alert

Initial Tags
curl, php

Initial Language
PHP