Return to Snippet

Revision: 45703
at May 5, 2011 16:43 by ReverentGhost


Initial Code
<?php
/* USE <?php echo $twitterFeed; ?>  TO OUTPUT THE FEED */ 

/* YOUR TWITTER USER INFORMATION */
$twitID = 'booshstudios';

/* TWITTER X TIME AGO CONVERSION */
function niceTime($time) {
  $delta = time() - $time;
  if ($delta < 60) {
    return 'less than a minute ago';
  } else if ($delta < 120) {
    return 'about a minute ago';
  } else if ($delta < (45 * 60)) {
    return floor($delta / 60) . ' minutes ago';
  } else if ($delta < (90 * 60)) {
    return 'about an hour ago';
  } else if ($delta < (24 * 60 * 60)) {
    return 'about ' . floor($delta / 3600) . ' hours ago';
  } else if ($delta < (48 * 60 * 60)) {
    return '1 day ago.';
  } else {
    return floor($delta / 86400) . ' days ago';
  }
}

/* GET TWITTER FEED FUNCTION */					
function getTwitterStatus($twitterUser, $howMany = 1) {
	/* ERROR HANDLING - DEFAULT IS SET AT 0 (OFF) */
	error_reporting(0);
	$url = sprintf("http://api.twitter.com/1/statuses/user_timeline/%s.xml?count=%d", $twitterUser, $howMany); 
	$feed_data_get = file_get_contents($url);
	if (!$feed_data_get) {
		// IF THE FEED IS UNAVAILABLE OUTPUT ERROR
		echo '<center>Feed  Unavailable, Try Again Later.</center>';
	} else {
		// IF IT IS AVAILABLE OUTPUT FEED
		$parsed = new SimpleXMLElement($feed_data_get);
		$tweets = array();
		foreach($parsed->status as $status) {
			$message = preg_replace("/http:\/\/(.*?)\/[^ ]*/", '<a href="\\0">\\0</a>',
			$status->text);
			$time = niceTime(strtotime(str_replace("+0000", "", $status->created_at)));
			$tweets[] = array('message' => $message, 'time' => $time);
		}
		return $tweets;
	}
}

/* TWITTER OUTPUT - Don't touch unless you know what you're doing. */
$status = getTwitterStatus($twitID);
$twitterFeed = ($status[0]['message']); 
?>

Initial URL
http://booshstudios.com

Initial Description


Initial Title
Twitter Feed w/ x-time-ago &  Error Handling

Initial Tags
error, api, twitter

Initial Language
PHP