SMS/Email Weather Alert


/ Published in: PHP
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. <?php
  2. $ch = curl_init();
  3. $timeout = 5; // set to zero for no timeout
  4. curl_setopt ($ch, CURLOPT_URL, 'http://www.weather.gov/alerts/wwarssget.php?zone=TNZ027');
  5. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  6. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  7. $contents = curl_exec($ch);
  8.  
  9. if(!empty($contents)) {
  10.  
  11. $db = new SQLiteDatabase('noaa.db2');
  12. /*
  13. $time = time();
  14. $db->query("BEGIN;
  15. CREATE TABLE status ( id INTEGER PRIMARY KEY, timestamp );
  16. INSERT INTO status (timestamp) VALUES($time);
  17. COMMIT;");
  18. */
  19. $result = $db->query('SELECT * FROM status WHERE id = 1 LIMIT 1',SQLITE_ASSOC); //SQLITE_NUM SQLITE_BOTH (Default)
  20. $data = $result->current();
  21. //echo $data['timestamp'];
  22.  
  23. $xml = new SimpleXMLElement($contents);
  24. foreach($xml->channel->item as $item) {
  25.  
  26. if (strstr(trim($item->title),'Tornado Warning')) {
  27.  
  28. if($data['timestamp'] < (time()-1800)) { // Set time in seconds between alerts
  29.  
  30. $db->query("UPDATE status SET timestamp = ".time()." WHERE id = 1");
  31.  
  32. require("phpmailer/class.phpmailer.php"); //http://sourceforge.net/projects/phpmailer
  33. $mail = new PHPMailer();
  34. $mail->IsSMTP(); // set mailer to use SMTP
  35. $mail->SMTPAuth = true; // turn on SMTP authentication
  36. $mail->Username = "xxxx"; // SMTP username
  37. $mail->Password = "xxxx"; // SMTP password
  38. $mail->SetLanguage("en",dirname(__FILE__) . "/phpmailer/language/");
  39. $mail->From = "[email protected]";
  40. $mail->FromName = "John Self";
  41. $mail->AddAddress("[email protected]");
  42. //$mail->AddAddress("[email protected]"); // Add as many recipients as you want
  43. $mail->IsHTML(false);
  44. $mail->Subject = "WEATHER ALERT";
  45. $mail->Body = "A TORNADO WARNING HAS BEEN ISSUED FOR DAVIDSON COUNTY";
  46. $mail->AltBody = "A TORNADO WARNING HAS BEEN ISSUED FOR DAVIDSON COUNTY";
  47.  
  48. if(!$mail->Send())
  49. {
  50. echo "Message could not be sent.
  51. ";
  52. echo "Mailer Error: " . $mail->ErrorInfo;
  53. }
  54. }
  55. }
  56. }
  57. }
  58. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.