Posted By

mloberg on 10/22/10


Tagged

php json twitter


Versions (?)

Who likes this?

16 people have marked this snippet as a favorite

nathanlong

codesniper81
pytheas
Desoxena
booski
laurentzziu
warren
drewbarden
vehler
HelloKit
czehner
sbcjr
amosk
clac
clacwebstudio


Twitter PHP Script


 / Published in: PHP
 

A Twitter pull-in that I coded using PHP. Uses the JSON user_timeline Twitter API call. It displays the tweet with links to any urls, users, or hashtags, the time ago, and if it was in reply to anyone.

  1. <?php
  2. // the number of tweets we want to display
  3. $tweetsToDisplay = 10;
  4. $username = "username"; // make sure you change this to your username
  5.  
  6. $twitterrequest = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=' . $username . '&count=' . $tweetsToDisplay;
  7. $twitterci = curl_init($twitterrequest);
  8. curl_setopt($twitterci,CURLOPT_RETURNTRANSFER, TRUE);
  9. $twitterinput = curl_exec($twitterci);
  10. curl_close($twitterci);
  11.  
  12. // parameter 'true' is necessary for output as PHP array
  13. $tweets = json_decode($twitterinput,true);
  14.  
  15. echo "<h3><a href=\"http://twitter.com/$username\">Twitter Updates</a></h3>";
  16.  
  17. if(empty($tweets)){
  18. echo "Twitter doesn't seem to be working at the moment.";
  19. }else if(!empty($tweets['error'])){
  20. echo "There was an issue with Twitter.";
  21. }else{
  22. // Twitter uses the GMT timezone
  23. date_default_timezone_set('GMT');
  24. // This is Twitter's response date format
  25. $curDate = date('D M d H:i:s O Y');
  26. // then we explode it to use it later
  27. list($curDay, $curMonth, $curDayNum, $curTime, $curTimezone, $curYear) = explode(" ",$curDate);
  28. $curTimeArray = explode(":", $curTime);
  29. // turn the month name into a number
  30. $curMonthNum = array("Filler","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
  31. for($cm=1;$cm<=12;$cm++){
  32. if($curMonthNum[$cm] == $curMonth){
  33. $curMonth = $cm;
  34. }
  35. }
  36. echo "<ul>";
  37. // a quick bug fix, without this extra bullets may be in the list
  38. if($tweets[0]['user']['statuses_count'] < $tweetsToDisplay){
  39. $display = $tweets[0]['user']['statuses_count'] - 1;
  40. }else{
  41. $display = $tweetsToDisplay - 1;
  42. }
  43.  
  44. for($i=0;$i<=$display;$i++){
  45. echo "<li>";
  46. // we are going to check for any links, @users, or #hashtags
  47. $tweetText = $tweets[$i]['text'] . "<br />";
  48. if(preg_match('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@',$tweetText,$urlTweet)){
  49. // if there is, replace that match with an actual link
  50. $tweetText = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@','<a href="$1">$1</a>',$tweetText);
  51. }
  52. // check to see if it's a reply
  53. if(preg_match('/@([\w]+)/', $tweetText)){
  54. $tweetText = preg_replace('/@([\w]+)/','<a href="http://twitter.com/$1">@$1</a>',$tweetText);
  55. }
  56. // then check for a hash tag
  57. if(preg_match('/\B#(\w*[a-zA-Z]+\w*)/', $tweetText)){
  58. $tweetText = preg_replace('/\B#(\w*[a-zA-Z]+\w*)/', '<a href="http://twitter.com/search?q=%23$1">#$1</a>',$tweetText);
  59. }
  60. // Finally, we can echo the Tweet
  61. echo $tweetText;
  62. // then we figure out the time ago it was sent
  63. $tweetTime = $tweets[$i]['created_at'];
  64. list($day, $month, $dayNum, $time, $timezone, $year) = explode(" ", $tweetTime);
  65. $timeArray = explode(":",$time);
  66. // Because the month comes in as a string, we need to covert it to a integer
  67. $monthNum = array("Filler","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
  68. for($m=1;$m<=12;$m++){
  69. if($monthNum[$m] == $month){
  70. $month = $m;
  71. }
  72. }
  73. // and then echo it
  74. // if the month, day, and hour are the same, echo the minutes ago
  75. if($year == $curYear && $month == $curMonth && $dayNum == $curDayNum && $timeArray[0] == $curTimeArray[0]){
  76. $timeAgo = $curTimeArray[1] - $timeArray[1];
  77. if($timeAgo == 0){
  78. echo "Sent less then a minute ago";
  79. }else if($timeAgo == 1){
  80. echo "Sent one minute ago";
  81. }else if($timeAgo > 50){
  82. echo "Sent about an hour ago";
  83. }else{
  84. echo "Sent " . $timeAgo . " minutes ago";
  85. }
  86. // if it was less then a day ago, echo the hours ago
  87. }else if($year == $curYear && $month == $curMonth && $dayNum == $curDayNum){
  88. $timeAgo = $curTimeArray[0] - $timeArray[0];
  89. if($timeAgo > 22){
  90. echo "Sent about a day ago";
  91. }else if($timeAgo ==1){
  92. echo "Sent an hour ago";
  93. }else{
  94. echo "Sent " . $timeAgo . " hours ago";
  95. }
  96. }else if($year == $curYear && $month == $curMonth){
  97. $timeAgo = $curDayNum - $dayNum;
  98. if($timeAgo > 29){
  99. echo "Sent about a month ago";
  100. }else if($timeAgo == 1){
  101. echo "Sent a day ago";
  102. }else{
  103. echo "Sent " . $timeAgo . " days ago";
  104. }
  105. }else if($year == $curYear){
  106. $timeAgo = $curMonth - $month;
  107. if($timeAgo == 1){
  108. echo "Sent one month ago";
  109. }else{
  110. echo "Sent " . $timeAgo . " months ago";
  111. }
  112. }else{
  113. $timeAgo = $curYear - $year;
  114. if($timeAgo == 1){
  115. echo "Sent " . $timeAgo . " year ago";
  116. }else{
  117. echo "Sent " . $timeAgo . " years ago";
  118. }
  119. }
  120. // if it's a reply, append a "In reply to at the end"
  121. if($tweets[$i]['in_reply_to_screen_name'] !== NULL){
  122. echo " in reply to <a href=\"http://twitter.com/" . $tweets[$i]['in_reply_to_screen_name'] . "\">" . $tweets[$i]['in_reply_to_screen_name'] . "</a>.";
  123. }else{
  124. echo ".";
  125. }
  126. echo "</li>";
  127. }
  128. echo "</ul>";
  129. echo "<p><a href=\"http://twitter.com/$username\">Follow Me</a></p>";
  130. }
  131. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: pepe288 on January 14, 2011

How show avatar image?

Posted By: jarc100 on February 18, 2011

i really don't know too mucho of PHP, this code should go in a separete file right? how do you call it into the HTML file?

Posted By: mloberg on February 19, 2011

If you want to use this file, you will have to either put the code right in the HTML (which you would have to save as .php), include() it using php, or use ajax to get the file.

Posted By: BrainScanMedia on December 31, 2013

This code does not work. I know Twitter removed this feature to grab tweets without a code.

You need to login to post a comment.