This recently created PHP class is part of today’s ongoing struggle of trying to create a Twitter ‘drip’ that will automatically update Twitter at pre-defined intervals with status updates specified in a text file.
// (C) Copyright JAMES PADOLSEY class tweetFromFile { private $curlHandle; private $updateFile; private $archiveFile; private function getNewStatus() { $upcomingTweetsFile = $this->files['upcoming']; $archivedTweetsFile = $this->files['archive']; $upcomingTweets_R = fopen($upcomingTweetsFile, "r"); // Get upcoming Tweets: $contents = fread($upcomingTweets_R, filesize($upcomingTweetsFile)); $splitContents = preg_split('/n/', $contents, 2); // ARCHIVE OLD POSTS: $archive = fopen($archivedTweetsFile, "a"); fwrite($archive, $splitContents[0]."n"); // Remove top line from upcoming: $upcomingTweets_W = fopen($upcomingTweetsFile, "w"); fwrite($upcomingTweets_W, $splitContents[1]); // Clean up fclose($upcomingTweets_W); fclose($upcomingTweets_R); fclose($archive); return $splitContents[0]; } public $files = array('upcoming' => '', 'archive' => ''); public function __construct($username, $password, $filename) { $this->curlHandle = curl_init(); $this->files['upcoming'] = $filename; $this->files['archive'] = 'ARCHIVE_' . $filename; // Shortcut: $ch = $this->curlHandle; curl_setopt($ch, CURLOPT_URL, "http://twitter.com/statuses/update.xml"); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); } public function __destruct() { curl_close($this->curlHandle); } public function updateStatus() { $status = $this->getNewStatus(); curl_setopt($this->curlHandle, CURLOPT_POSTFIELDS, "status=$status"); $result = curl_exec($this->curlHandle); $resultArray = curl_getinfo($this->curlHandle); if ($resultArray['http_code'] == 200) return true; return false; } } // ================== // ===== USAGE ====== // ================== $tweet = new tweetFromFile('twitterUsername', 'password', 'textfile.txt'); $success = $tweet->updateStatus(); if ($success) { echo 'Twitter updated!'; } else { echo 'Hmm, an error...'; } |
The class will retrieve each new update from the specified text file. Each update needs to be on a new line; an example text file:
Blah blah blah, this is the first update... And another update! Third and last update!!! ARGH! |
The PHP class will remove each update from this file when it’s added to Twitter so all you need to do is keep adding to the bottom of the file…
This won’t do the dripping automatically. You’ll need to setup a ‘cron job’ to request the script at selected intervals (e.g. every two hours). I’m rubbish at Apache and anything to do with the command line so I haven’t quite accomplished this part yet… maybe someone else wants to give it a go? 😉
Thanks for reading! Please share your thoughts with me on Twitter. Have a great day!
Wow pretty awesome! Thanks James – even if I can hardily understand the code!
Jack.
No need to use Apache. If you want to call a page at a fixed interval, use ‘crontab -e’ to edit your crontab adn add a line similar to this:
This particular crontab entry works every 10 minutes (on the X:00, X:10, X:20, etc). You should have wget, but if not, it’s easy to install.
@Robert That would indeed work, but your crontab entry could be tidied up a little:
Personally, and for maximum functionality I would write a bash script to use php5-cli to execute the php file, and send the outputs to log files. The script could still be used by web-side apps in the normal manner.
chron job for sure
but if you feel like testing it in a browser window you can always use PHP’s usleep http://ca2.php.net/usleep
this works perfect to me! now i can publish emails that i dont want to read myself to twitter 🙂