/ Published in: PHP
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.
Expand |
Embed | Plain Text
<?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); $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) { 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->From = "alerts@johnself.com"; $mail->FromName = "John Self"; $mail->AddAddress("email1@internet.com"); //$mail->AddAddress("email2@internet.com"); // 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. "; exit; } exit; } } } } ?>
Comments
Subscribe to comments
You need to login to post a comment.

Nice one John!