Created
February 25, 2011 14:42
-
-
Save joshhartman/843873 to your computer and use it in GitHub Desktop.
Get Recent Tweets by Username from Twitter API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class TwitterFeed { | |
private var $feed_url; | |
private var $feed_data; | |
public function __construct($username, $limit=5){ | |
date_default_timezone_set('America/Chicago'); | |
$this->feed_url = "http://search.twitter.com/search.atom?q=from:".$username."&rpp=".$limit; | |
$this->feed_data = simplexml_load_file($this->feed_url); | |
} | |
private function twitter_time_format($date) { | |
$blocks = array ( | |
array('year', (3600 * 24 * 365)), | |
array('month', (3600 * 24 * 30)), | |
array('week', (3600 * 24 * 7)), | |
array('day', (3600 * 24)), | |
array('hour', (3600)), | |
array('min', (60)), | |
array('sec', (1)) | |
); | |
#Get the time from the function arg and the time now | |
$argtime = strtotime($date); | |
$nowtime = time(); | |
#Get the time diff in seconds | |
$diff = $nowtime - $argtime; | |
#Store the results of the calculations | |
$res = array (); | |
#Calculate the largest unit of time | |
for ($i = 0; $i < count($blocks); $i++) { | |
$title = $blocks[$i][0]; | |
$calc = $blocks[$i][1]; | |
$units = floor($diff / $calc); | |
if ($units > 0) { | |
$res[$title] = $units; | |
} | |
} | |
if (isset($res['year']) && $res['year'] > 0) { | |
if (isset($res['month']) && $res['month'] > 0 && $res['month'] < 12) { | |
$format = "About %s %s %s %s ago"; | |
$year_label = $res['year'] > 1 ? 'years' : 'year'; | |
$month_label = $res['month'] > 1 ? 'months' : 'month'; | |
return sprintf($format, $res['year'], $year_label, $res['month'], $month_label); | |
} else { | |
$format = "About %s %s ago"; | |
$year_label = $res['year'] > 1 ? 'years' : 'year'; | |
return sprintf($format, $res['year'], $year_label); | |
} | |
} | |
if (isset($res['month']) && $res['month'] > 0) { | |
if (isset($res['day']) && $res['day'] > 0 && $res['day'] < 31) { | |
$format = "About %s %s %s %s ago"; | |
$month_label = $res['month'] > 1 ? 'months' : 'month'; | |
$day_label = $res['day'] > 1 ? 'days' : 'day'; | |
return sprintf($format, $res['month'], $month_label, $res['day'], $day_label); | |
} else { | |
$format = "About %s %s ago"; | |
$month_label = $res['month'] > 1 ? 'months' : 'month'; | |
return sprintf($format, $res['month'], $month_label); | |
} | |
} | |
if (isset($res['day']) && $res['day'] > 0) { | |
if ($res['day'] == 1) { | |
return sprintf("Yesterday at %s", date('h:i a', $argtime)); | |
} | |
if ($res['day'] <= 7) { | |
return date("\L\a\s\\t l \a\\t h:i a", $argtime); | |
} | |
if ($res['day'] <= 31) { | |
return date("l \a\\t h:i a", $argtime); | |
} | |
} | |
if (isset($res['hour']) && $res['hour'] > 0) { | |
if ($res['hour'] > 1) { | |
return sprintf("About %s hours ago", $res['hour']); | |
} else { | |
return "About an hour ago"; | |
} | |
} | |
if (isset($res['min']) && $res['min']) { | |
if ($res['min'] == 1) { | |
return "About one minut ago"; | |
} else { | |
return sprintf("About %s minutes ago", $res['min']); | |
} | |
} | |
if (isset ($res['sec']) && $res['sec'] > 0) { | |
if ($res['sec'] == 1) { | |
return "One second ago"; | |
} else { | |
return sprintf("%s seconds ago", $res['sec']); | |
} | |
} | |
} | |
private function xmldecode($txt){ | |
$txt = str_replace('&', '&', $txt); | |
$txt = str_replace('<', '<', $txt); | |
$txt = str_replace('>', '>', $txt); | |
$txt = str_replace(''', "'", $txt); | |
$txt = str_replace('"', '"', $txt); | |
return $txt; | |
} | |
public function get_tweets(){ | |
$output = '<ul class="tweets">'; | |
$tweets = ''; | |
foreach($this->feed_data->entry as $entry){ | |
$tweets .= '<li class="tweet-entry"><span class="tweet-content">'.$this->xmldecode($entry->content).'</span><br/><span class="tweet-time">'.$this->twitter_time_format($entry->updated).'</span></li>'; | |
} | |
$output .= ($tweets) ? $tweets : '<li>No recent tweets were found for this user.</li>'; | |
$output .= '</ul>'; | |
return $output; | |
} | |
} | |
?> | |
<html> | |
<head> | |
<title>Twitter Status</title> | |
</head> | |
<body> | |
<h1>Latest Tweets</h1> | |
<?php | |
$twitter_feed = new TwitterFeed('abcnews'); | |
echo $twitter_feed->get_tweets(); | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment