Push Twitter Replies to your iPhone using Prowl
Yesterday I bought Prowl for the iPhone. Prowl is similar to Growl on OSX systems. Prowl pushes notifications to your iPhone or iTouch. One of the first uses I thought to use Prowl for was pushing my @replies and mentions from Twitter to my iPhone. I wanted to run the script from my Ubuntu server rather then keeping my desktop on 24/7. I did a quick search online and didn't find any command line options for achieving such a task. I decided to code a quick and dirty PHP script to accomplish what I wanted to do.
This is a quick hack. There are probably more efficient ways of accomplishing this task. I used ProwlPHP to link to the Prowl API.
I assume you know PHP and know how to navigate in Terminal. This is a command line app, it will run on any computer/server running PHP. I have extensively commented the script. Hopefully it is easy to follow along.
Quick Instructions:
- Download the newest version of ProwlPHP and copy it a directory.
- Create a file lastreply.txt and stick in in the same directory as the below code will be copied into. This file needs to have read and write permissions.
- Copy the below code into another file. Alter the ProwlPHP includes directory on line 2, and add your Prowl API key, Twitter Username, and Twitter Password in the constructor.
//CHANGE THIS PATH TO WHERE ProwlPHP IS LOCATED ON //YOUR SERVER include('../API/ProwlPHP.php'); $t = new Twitter(); /** * @class Twitter * This class integrates Twitter and Prowl notifications with the * iPhone. When this script runs it checks to see if any new * Twitter mentions have occured on your account since last check. * If any exist, the Tweet is sent to Prowl and will be notified * via push on your iPhone. * * This class uses ProwlPHP located at: * http://github.com/Fenric/ProwlPHP * * @author Kastang (josh dot kastang at gmail dot com) */ class Twitter { var $xml; var $lastID; var $tUser; var $tPass; var $prowl; /** * Constructor for Twitter Class. Three lines need to be edited * below before running the file: prowl, tUser, and tPass. */ function __construct() { //EDIT THE 3 LINES BELOW $this->prowl = new Prowl('YOUR PROWL API KEY'); $this->tUser = "TWITTER USERNAME"; $this->tPass = "TWITTER PASSWORD"; //XML info loaded from Twitter API. $this->xml = simplexml_load_string($this->getReplies()); //Opens the lastreply file which contants the //id of the last mentioned tweet. $this->lastID = file_get_contents("lastreply.txt"); //If the file is empty (probably the first time //you are using the script). It will pull the //newest mention id from your twitter feed and //store it in the file. if ($this->lastID == null) { $this->lastID = $this->xml->status[0]->id; $full = "@" . $this->xml->status[0]->user->screen_name . ": " . $this->xml->status[0]->text; $this->prowl($full); $this->updateNewest($this->xml->status[0]->id); } //Checks for new Twitter Mentions. $this->checkForUpdates(); } /** * Checks for updates. */ function checkForUpdates() { //first run boolean. $first = true; //For each mention in the XML array, check to see if //the current ID is greater then the last recorded ID. //If it is, push the current Tweet to Prowl, if it isn't, //check to see if it is the first run, if it is, break out //of the for loop, if it isn't the first run, update the //lastreply.txt file and break from the forloop. for ($i = 0; $i < 10; $i++) { $curr = $this->xml->status[$i]->id; if ($curr > $this->lastID) { $first = false; $full = "@" . $this->xml->status[$i]->user->screen_name . ": " . $this->xml->status[$i]->text; $this->prowl($full); } else { if ($first) { break; } else { $this->updateNewest($this->xml->status[($i - 1)]->id); break; } } } } /** * Writes the newest @reply id to a file. */ function updateNewest($id) { $file = fopen("lastreply.txt", "w"); fwrite($file, $id); fclose($file); } /** * Push the Tweet to Prowl. This code is modified from * example.php in the ProwlPHP API Wrapper. */ function prowl($tweet) { $this->prowl->push(array( 'application' => 'Twitter', 'event' => 'Reply', 'description' => $tweet, 'priority' => 0, ), true); } /** * Gets replies from Twitter. In order to grab replies, you * must be authenticated. */ function getReplies() { $twitterHost = "http://twitter.com/statuses/mentions.xml"; $curl; $curl = curl_init(); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_USERPWD, "$this->tUser:$this->tPass"); curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($curl, CURLOPT_URL, $twitterHost); $result = curl_exec($curl); curl_close($curl); header('Content-Type: application/xml; charset=ISO-8859-1'); return $result; } }
I chose to add an entry in my crontab to run this script every 10 minutes. The time can be adjusted to suit your needs. Personally I do not see a need to ping Twitter more then once every ten minutes.
*/10 * * * * curl /path/to/replies.php
If all goes well, you should see something like this when someone mentions you in a tweet:
It is possible to setup a redirection within Prowl to automatically launch your Twitter client of choice when you get a Twitter based notification. You can also open Prowl and view all notifications:
The script should be fairly easy to modify. I will probably add Direct Messaging next to my Prowl Push Notifications.



ScifiToday