Skip to content

Instantly share code, notes, and snippets.

@gegere
Last active December 11, 2015 01:49
Show Gist options
  • Save gegere/4526398 to your computer and use it in GitHub Desktop.
Save gegere/4526398 to your computer and use it in GitHub Desktop.
Would you like to display your recent tweets? This PHP class will help you get this job done. This class uses the JSON format.
<?php
class RecentTweets{
private $username;
const cache = '/cache/';
public function __construct($username){
$this->username = $username;
}
/* The get method returns an array of tweet objects */
public function get(){
$cache = $_SERVER['DOCUMENT_ROOT'] . self::cache . $this->username . '-twitter-cache';
$tweets = array();
if(file_exists($cache) && time() - filemtime($cache) < 3*60*60){
// Use the cache if it exists and is less than three hours old
$tweets = unserialize(file_get_contents($cache));
}
else{
// Otherwise rebuild it
$tweets = json_decode($this->fetch_feed());
file_put_contents($cache,serialize($tweets));
}
if(!$tweets){
$tweets = array();
}
return $tweets;
}
/* The generate method takes an array of tweets and build the markup */
public function generate($limit=10, $className = 'tweets'){
echo '<div id="'.$className.'">';
// Limiting the number of shown tweets
$tweets = array_slice($this->get(),0,$limit);
foreach($tweets as $t){
$id = $t->id_str;
$text = self::formatTweet($t->text);
$time = self::relativeTime($t->created_at);
$username = $t->user->screen_name;
$retweets = $t->retweet_count;
?>
<blockquote>
<p><?php echo $text ?></p>
<div class="info">
<?php if($retweets > 0):?>
<span class="retweet" title="Retweet Count"><?php echo $retweets ?> retweets</span>
<?php endif;?>
<a href="http://twitter.com/<?php echo $username,'/status/',$id?>" class="date" target="_blank" title="Shared <?php echo $time?>">
<br /><small><?php echo $time?></small></a>
</div>
</blockquote>
<?php
}
echo "</div>";
}
/* Helper methods and static functions */
private function fetch_feed(){
return file_get_contents("https://api.twitter.com/1/statuses/user_timeline.json?screen_name={$this->username}&count={$this->limit}");
}
private static function relativeTime($time){
$divisions = array(1,60,60,24,7,4.34,12);
$names = array('second','minute','hour','day','week','month','year');
$time = time() - strtotime($time);
$name = "";
if($time < 10){
return "just now";
}
for($i=0; $i<count($divisions); $i++){
if($time < $divisions[$i]) break;
$time = $time/$divisions[$i];
$name = $names[$i];
}
$time = round($time);
if($time != 1){
$name.= 's';
}
return "$time $name ago";
}
private static function formatTweet($str){
// Linkifying URLs, mentionds and topics. Notice that
// each resultant anchor type has a unique class name.
$str = preg_replace(
'/((ftp|https?):\/\/([-\w\.]+)+(:\d+)?(\/([\w\/_\.]*(\?\S+)?)?)?)/i',
'<a class="link" href="$1" target="_blank">$1</a>',
$str
);
$str = preg_replace(
'/(\s|^)@([\w\-]+)/',
'$1<a class="mention" href="http://twitter.com/#!/$2" target="_blank">@$2</a>',
$str
);
$str = preg_replace(
'/(\s|^)#([\w\-]+)/',
'$1<a class="hash" href="http://twitter.com/search?q=%23$2">#$2</a>',
$str
);
return $str;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment