Posted By

johnself on 02/06/08


Tagged

curl php php5 simplexml cron SQLite phpmailer


Versions (?)

Who likes this?

15 people have marked this snippet as a favorite

trey
luman
jonhenshaw
jaytee
jeffreality
Akiva
irishsk
nb109
Hollow
BrianCoyDesign
bobbym245
mecha
hobbyman
Almorca
prit


SMS/Email Weather Alert


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

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

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: jonhenshaw on February 7, 2008

Nice one John!

You need to login to post a comment.