Send Growl Notifications Using PHP
Most websites have alert systems built in that can notify you whenever something goes wrong – a database crash or Apache error for example. Often times they’ll send an email, save the problem to a log file, or even send an instant message. I’ve seen some systems that will page your cellphone. Most of the sites I build notify me via RSS.
All these methods are perfectly fine and have their place, but I wanted to tie my websites into the Growl notifier on my Mac. This appeals to me for a couple reason. First, it’s more immediate than email or RSS. My newsreader refreshes every thirty minutes, and my mail client checks every ten. If my database crashes or there’s some other major problem on my site I want to know about it now. On the other hand, Growl notifications are passive. They pop up on my screen, and if the message isn’t important they disappear a few seconds later without me having to intervene. Instant messages are more distracting. They open up a whole new window that I have to switch to and close. With Growl I can quickly glance at the notification and do something about it or keep working at my current task.
Typically, Growl notifications are sent by programs running locally on your computer. Version 0.7 introduced support for network notifications. I’ve rarely heard anyone talk about them, and seen them used even less. But the idea is simple. A computer on your local network gets a Growl alert from one of its programs and forwards it to your machine. For example, at home, I have a Mac Mini connected to my living room TV. It’s set to automatically download new episodes of my favorites shows and sends my laptop a notice when one finishes downloading.
The Growl network protocol is documented on their website and pretty simple to implement. I’ve written a PHP wrapper around it that you can drop into your website and use to send notifications to your local machine. Here’s a quick example of how to register your Growl app with your local computer:
$g = new Growl("My Web Service");
Once you’ve done that, Growl is ready to receive notifications from your website:
$g->notify("Warning", "Database Error Occurred", "Error details go here.");
If all goes well, a notification should pop-up on your machine almost instantly.
If you’d like to see a bigger example of the Growl class in action, I’ve written a Mint Pepper plugin that notifies you whenever certain events (like new referrals, unique visitors, etc) happen on your site. It’s a quick way to stay on top of who is visiting your site. Check out the details here.