From MWWiki
Back to Main Project Page
<?php
include_once('update.php');
include_once('format.php');
include_once('logger.php');
// this number takes into account:
// 1) Twitter API rate limitations
// 2) crontab frequency
$NUMHITS = 2;
// create the loggers
$errors = new Logger('publictimeline.log');
$permanent = new Logger('permanent.log');
// grab the public timeline and post the entries to the file
$t = new Update();
$posts = $t->getPublicTimeline($NUMHITS);
// append each post to the file
$string = '';
// loop through each result
for ($i = 0; $i < $NUMHITS; $i++) {
$numPosts = count($posts[$i]);
// loop through each collection of 20 posts
for ($j = 0; $j < $numPosts; $j++) {
$post = $posts[$i][$j];
// log the post and format it
$permanent->write($post->user->screen_name . ' :: ' .
$post->text . "\n", 'POST');
$string .= Format::formatPost($post->text);
}
}
// open the file where we'll write the posts
$fp = @fopen('timeline.txt', 'a+');
if (!$fp) {
$errors->write('Unable to open timeline file for appending.', Logger::$ERROR);
exit;
}
if (flock($fp, LOCK_EX)) {
fwrite($fp, $string);
flock($fp, LOCK_UN);
} else {
$errors->write('Unable to lock timeline file for appending.', Logger::$ERROR);
}
// close the timeline file, we're done
fclose($fp);
?>