Skip to content

Instantly share code, notes, and snippets.

@stephenyeargin
Created March 14, 2012 18:30
Show Gist options
  • Save stephenyeargin/2038493 to your computer and use it in GitHub Desktop.
Save stephenyeargin/2038493 to your computer and use it in GitHub Desktop.
Nashville pollen cron job
<?php
// Config
define('POLLEN_THRESHOLD', 15);
define('EMAIL_RECIPIENT', 'stephen@example.com');
/*
0 to 5 Pollen Grains/cm = Slight
6 to 15 Pollen Grains/cm = Moderate
16 to 25 Pollen Grains/cm = Heavy
Greater than 25 Pollen Grains/cm = Extremely Heavy
Example:
stdClass Object
(
[date] => 2012-03-14
[count] => 161
[types] => Cedar, Maple, Pine
[level] => Extremely Heavy
)
*/
// Get the data, decode it
$url = 'http://jasontan.org/pollen.php';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$data = json_decode($result);
// If threshold not met, die.
if ($data->count <= (int) POLLEN_THRESHOLD)
die('Pollen is fine. No e-mail to send.' . PHP_EOL);
if ($data->date != date('Y-m-d'))
die('Data was not posted for today yet.' . PHP_EOL);
// Build message
$subject = sprintf('[Pollen] %s - %s (%s grains/cm)', $data->level, $data->types, $data->count);
$message = 'You should probably take some medicine.';
$stat = mail(EMAIL_RECIPIENT, $subject, $message);
if ($stat)
die('Sent!' . PHP_EOL);
else
die('Email was not sent.' . PHP_EOL);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment