Posted By

builtnoble on 07/03/11


Tagged

twitter tweets


Versions (?)

Twitter Class


 / Published in: PHP
 

Based off gripnrip's original twitter status script

  1. class Twitter {
  2.  
  3. /**
  4. * Method to make twitter api call for the users timeline in XML
  5. *
  6. * @access private
  7. * @param $twitter_id, $num_of_tweets
  8. * @return $xml
  9. */
  10. private function api_call($twitter_id, $num_of_tweets) {
  11. $c = curl_init();
  12.  
  13. curl_setopt($c, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/$twitter_id.xml?count=$num_of_tweets");
  14. curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
  15. curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 3);
  16. curl_setopt($c, CURLOPT_TIMEOUT, 5);
  17.  
  18. $response = curl_exec($c);
  19. $response_info = curl_getinfo($c);
  20.  
  21. curl_close($c);
  22.  
  23. if (intval($response_info['http_code']) == 200) {
  24. $xml = new SimpleXMLElement($response);
  25.  
  26. return $xml;
  27. } else {
  28. return false;
  29. }
  30. }
  31.  
  32. /**
  33. * Method to add hyperlink html tags to any urls, twitter ids or hashtags in tweet
  34. *
  35. * @access private
  36. * @param $text
  37. * @return $text
  38. */
  39. private function process_links($text) {
  40. $text = utf8_decode($text);
  41. $text = preg_replace('@(https?://([-\w\.]+)+(d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $text);
  42. $text = preg_replace("#(^|[\n ])@([^ \"\t\n\r<]*)#ise", "'\\1<a href=\"http://www.twitter.com/\\2\" >@\\2</a>'", $text);
  43. $text = preg_replace("#(^|[\n ])\#([^ \"\t\n\r<]*)#ise", "'\\1<a href=\"http://hashtags.org/search?query=\\2\" >#\\2</a>'", $text);
  44.  
  45. return $text;
  46. }
  47.  
  48. /**
  49. * Main method to retrieve the tweets and return html for display
  50. *
  51. * @access public
  52. * @param $twitter_id, $num_of_tweets, $timezone
  53. * @return $result
  54. */
  55. public function get_tweets($twitter_id, $num_of_tweets = 3, $timezone = "America/Denver") {
  56. $include_replies = false;
  57.  
  58. date_default_timezone_set($timezone);
  59.  
  60. // the html markup
  61. $cont_o = "<div id=\"tweets\">\n";
  62.  
  63. $tweet_o = "<div class=\"status\">\n";
  64. $tweet_c = "</div>\n\n";
  65.  
  66. $detail_o = "<div class=\"details\">\n";
  67. $detail_c = "</div>\n\n";
  68.  
  69. $cont_c = "</div>\n";
  70.  
  71. if ($twitter_xml = $this->api_call($twitter_id, $num_of_tweets)) {
  72. $result = $cont_o;
  73.  
  74. foreach ($twitter_xml->status as $key => $status) {
  75. if ($include_replies == true | substr_count($status->text, "@") == 0 | strpos($status->text, "@") != 0) {
  76. $tweet = $this->process_links($status->text);
  77.  
  78. $result .= $tweet_o . $tweet . $tweet_c . $detail_o . date('D jS M y H:i', strtotime($status->created_at)) . $detail_c;
  79. }
  80. }
  81.  
  82. $result .= $cont_c;
  83. } else {
  84. $result .= $cont_o . $tweet_o . "Twitter seems to be unavailable at the moment." . $tweet_c . $cont_c;
  85. }
  86.  
  87. return $result;
  88. }
  89.  
  90. }

Report this snippet  

You need to login to post a comment.