Posted By

gripnrip on 06/23/09


Tagged

php html twitter tweets


Versions (?)

Who likes this?

8 people have marked this snippet as a favorite

luman
irishsk
bsides
darylclaudio
jens89
crishnakh
damarev
ringo380


Get Twitter Tweets


 / Published in: PHP
 

Some PHP code that can be used to get formatted HTML containing N number of tweets for a given twitter user.

  1. <?php
  2. /*
  3. Description: Twitter PHP code
  4. Author: Andrew MacBean
  5. Version: 1.0.0
  6. */
  7.  
  8. /** Method to make twitter api call for the users timeline in XML */
  9. function twitter_status($twitter_id) {
  10. $c = curl_init();
  11. curl_setopt($c, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/$twitter_id.xml");
  12. curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
  13. curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 3);
  14. curl_setopt($c, CURLOPT_TIMEOUT, 5);
  15. $response = curl_exec($c);
  16. $responseInfo = curl_getinfo($c);
  17. curl_close($c);
  18. if (intval($responseInfo['http_code']) == 200) {
  19. if (class_exists('SimpleXMLElement')) {
  20. $xml = new SimpleXMLElement($response);
  21. return $xml;
  22. } else {
  23. return $response;
  24. }
  25. } else {
  26. return false;
  27. }
  28. }
  29.  
  30. /** Method to add hyperlink html tags to any urls, twitter ids or hashtags in the tweet */
  31. function processLinks($text) {
  32. $text = utf8_decode( $text );
  33. $text = preg_replace('@(https?://([-\w\.]+)+(d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $text );
  34. $text = preg_replace("#(^|[\n ])@([^ \"\t\n\r<]*)#ise", "'\\1<a href=\"http://www.twitter.com/\\2\" >@\\2</a>'", $text);
  35. $text = preg_replace("#(^|[\n ])\#([^ \"\t\n\r<]*)#ise", "'\\1<a href=\"http://hashtags.org/search?query=\\2\" >#\\2</a>'", $text);
  36. return $text;
  37. }
  38.  
  39. /** Main method to retrieve the tweets and return html for display */
  40. function get_tweets($twitter_id,
  41. $nooftweets=3,
  42. $dateFormat="D jS M y H:i",
  43. $includeReplies=false, $dateTimeZone="Europe/London",
  44. $beforeTweetsHtml="<ul>",
  45. $tweetStartHtml="<li class=\"tweet\"><span class=\"tweet-status\">",
  46. $tweetMiddleHtml="</span><br/><span class=\"tweet-details\">",
  47. $tweetEndHtml="</span></li>",
  48. $afterTweetsHtml="</ul>") {
  49.  
  50. date_default_timezone_set($dateTimeZone);
  51. if ( $twitter_xml = twitter_status($twitter_id) ) {
  52. $result = $beforeTweetsHtml;
  53. foreach ($twitter_xml->status as $key => $status) {
  54. if ($includeReplies == true | substr_count($status->text,"@") == 0 | strpos($status->text,"@") != 0) {
  55. $message = processLinks($status->text);
  56. $result.=$tweetStartHtml.$message.$tweetMiddleHtml.date($dateFormat,strtotime($status->created_at)).$tweetEndHtml;
  57. ++$i;
  58. if ($i == $nooftweets) break;
  59. }
  60. }
  61. $result.=$afterTweetsHtml;
  62. }
  63. else {
  64. $result.= $beforeTweetsHtml."<li id='tweet'>Twitter seems to be unavailable at the moment</li>".$afterTweetsHtml;
  65. }
  66. echo $result;
  67. }
  68. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: bsides on December 11, 2009

Hello, for some reason, whenever I call get_tweets($user) I get the list but only with the dates and no tweets themselves. The code is unchanged, I'm just calling the funciont with a var which is a valid twitter name (like $user = "bsides";). Please help! :)

Posted By: bsides on December 11, 2009

Ok I got it working. The error in your script is at line 55. The var $message doesn't have anything. To fix this, just replace line 55 with:

$message = processLinks($status->text);

Now I'm working around foreign signs and encoding issues, but overall, great script. Thanks a lot :)

Posted By: gripnrip on May 11, 2010

Only just notices these comments, my bad, your completely correct and its now fixed. cheers.

Posted By: askar on September 24, 2010

hello .i hope you are doing well sir.sir i have a problem .i have you used your code.for the first time is was runnig.tweets were showing that time .but not got a message from the else portion that "Twitter seems to be unavailable at the moment". sir is the possible reasons for this message.how can i get rid of this message.plz sir give me some solution to this .i am new to twitter.i am waiting for kind and instant reply: Regards ASkar khan

Posted By: askar on September 24, 2010

hello .i hope you are doing well sir.sir i have a problem .i have you used your code.for the first time is was runnig.tweets were showing that time .but not got a message from the else portion that "Twitter seems to be unavailable at the moment". sir is the possible reasons for this message.how can i get rid of this message.plz sir give me some solution to this .i am new to twitter.i am waiting for kind and instant reply: Regards ASkar khan

Posted By: askar on September 24, 2010

hello .i hope you are doing well .sir i have a problem .i have used your code.for the first time is was runnig.tweets were showing that time .but now i got a message from the else portion that "Twitter seems to be unavailable at the moment". sir is there any possible reasons for this message.how can i get rid of this message.plz sir give me some solution to this .i am new to twitter.i am waiting for kind and instant reply: Regards ASkar khan

Posted By: f6design on October 8, 2010

Thanks for this. Super stuff. I borrowed your hashtag/url parsing for my own Twitter RSS parser: http://f6design.com/journal/2010/10/07/display-recent-twitter-tweets-using-php/ (and gave you credit of course).

One thing to note: Twitter limits the number of times a user client can fetch their RSS feed per hour, so on a busy website this limit will be quickly reached. The way around this is to cache the tweets for one hour after they are fetched.

You need to login to post a comment.