Posted By

ReverentGhost on 05/05/11


Tagged

time feed error Handling ago api twitter


Versions (?)

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


 / Published in: PHP
 

URL: http://booshstudios.com

  1. <?php
  2. /* USE <?php echo $twitterFeed; ?> TO OUTPUT THE FEED */
  3.  
  4. /* YOUR TWITTER USER INFORMATION */
  5. $twitID = 'booshstudios';
  6.  
  7. /* TWITTER X TIME AGO CONVERSION */
  8. function niceTime($time) {
  9. $delta = time() - $time;
  10. if ($delta < 60) {
  11. return 'less than a minute ago';
  12. } else if ($delta < 120) {
  13. return 'about a minute ago';
  14. } else if ($delta < (45 * 60)) {
  15. return floor($delta / 60) . ' minutes ago';
  16. } else if ($delta < (90 * 60)) {
  17. return 'about an hour ago';
  18. } else if ($delta < (24 * 60 * 60)) {
  19. return 'about ' . floor($delta / 3600) . ' hours ago';
  20. } else if ($delta < (48 * 60 * 60)) {
  21. return '1 day ago.';
  22. } else {
  23. return floor($delta / 86400) . ' days ago';
  24. }
  25. }
  26.  
  27. /* GET TWITTER FEED FUNCTION */
  28. function getTwitterStatus($twitterUser, $howMany = 1) {
  29. /* ERROR HANDLING - DEFAULT IS SET AT 0 (OFF) */
  30. $url = sprintf("http://api.twitter.com/1/statuses/user_timeline/%s.xml?count=%d", $twitterUser, $howMany);
  31. $feed_data_get = file_get_contents($url);
  32. if (!$feed_data_get) {
  33. // IF THE FEED IS UNAVAILABLE OUTPUT ERROR
  34. echo '<center>Feed Unavailable, Try Again Later.</center>';
  35. } else {
  36. // IF IT IS AVAILABLE OUTPUT FEED
  37. $parsed = new SimpleXMLElement($feed_data_get);
  38. $tweets = array();
  39. foreach($parsed->status as $status) {
  40. $message = preg_replace("/http:\/\/(.*?)\/[^ ]*/", '<a href="\\0">\\0</a>',
  41. $status->text);
  42. $time = niceTime(strtotime(str_replace("+0000", "", $status->created_at)));
  43. $tweets[] = array('message' => $message, 'time' => $time);
  44. }
  45. return $tweets;
  46. }
  47. }
  48.  
  49. /* TWITTER OUTPUT - Don't touch unless you know what you're doing. */
  50. $status = getTwitterStatus($twitID);
  51. $twitterFeed = ($status[0]['message']);
  52. ?>

Report this snippet  

You need to login to post a comment.