Twitter PHP脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Twitter 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. // This is Twitter's response date format
  24. $curDate = date('D M d H:i:s O Y');
  25. // then we explode it to use it later
  26. list($curDay, $curMonth, $curDayNum, $curTime, $curTimezone, $curYear) = explode(" ",$curDate);
  27. $curTimeArray = explode(":", $curTime);
  28. // turn the month name into a number
  29. $curMonthNum = array("Filler","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
  30. for($cm=1;$cm<=12;$cm++){
  31. if($curMonthNum[$cm] == $curMonth){
  32. $curMonth = $cm;
  33. }
  34. }
  35. echo "<ul>";
  36. // a quick bug fix, without this extra bullets may be in the list
  37. if($tweets[0]['user']['statuses_count'] < $tweetsToDisplay){
  38. $display = $tweets[0]['user']['statuses_count'] - 1;
  39. }else{
  40. $display = $tweetsToDisplay - 1;
  41. }
  42.  
  43. for($i=0;$i<=$display;$i++){
  44. echo "<li>";
  45. // we are going to check for any links, @users, or #hashtags
  46. $tweetText = $tweets[$i]['text'] . "<br />";
  47. if(preg_match('@(https?://([-w.]+)+(:d+)?(/([w/_.]*(?S+)?)?)?)@',$tweetText,$urlTweet)){
  48. // if there is, replace that match with an actual link
  49. $tweetText = preg_replace('@(https?://([-w.]+)+(:d+)?(/([w/_.]*(?S+)?)?)?)@','<a href="$1">$1</a>',$tweetText);
  50. }
  51. // check to see if it's a reply
  52. if(preg_match('/@([w]+)/', $tweetText)){
  53. $tweetText = preg_replace('/@([w]+)/','<a href="http://twitter.com/$1">@$1</a>',$tweetText);
  54. }
  55. // then check for a hash tag
  56. if(preg_match('/B#(w*[a-zA-Z]+w*)/', $tweetText)){
  57. $tweetText = preg_replace('/B#(w*[a-zA-Z]+w*)/', '<a href="http://twitter.com/search?q=%23$1">#$1</a>',$tweetText);
  58. }
  59. // Finally, we can echo the Tweet
  60. echo $tweetText;
  61. // then we figure out the time ago it was sent
  62. $tweetTime = $tweets[$i]['created_at'];
  63. list($day, $month, $dayNum, $time, $timezone, $year) = explode(" ", $tweetTime);
  64. $timeArray = explode(":",$time);
  65. // Because the month comes in as a string, we need to covert it to a integer
  66. $monthNum = array("Filler","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
  67. for($m=1;$m<=12;$m++){
  68. if($monthNum[$m] == $month){
  69. $month = $m;
  70. }
  71. }
  72. // and then echo it
  73. // if the month, day, and hour are the same, echo the minutes ago
  74. if($year == $curYear && $month == $curMonth && $dayNum == $curDayNum && $timeArray[0] == $curTimeArray[0]){
  75. $timeAgo = $curTimeArray[1] - $timeArray[1];
  76. if($timeAgo == 0){
  77. echo "Sent less then a minute ago";
  78. }else if($timeAgo == 1){
  79. echo "Sent one minute ago";
  80. }else if($timeAgo > 50){
  81. echo "Sent about an hour ago";
  82. }else{
  83. echo "Sent " . $timeAgo . " minutes ago";
  84. }
  85. // if it was less then a day ago, echo the hours ago
  86. }else if($year == $curYear && $month == $curMonth && $dayNum == $curDayNum){
  87. $timeAgo = $curTimeArray[0] - $timeArray[0];
  88. if($timeAgo > 22){
  89. echo "Sent about a day ago";
  90. }else if($timeAgo ==1){
  91. echo "Sent an hour ago";
  92. }else{
  93. echo "Sent " . $timeAgo . " hours ago";
  94. }
  95. }else if($year == $curYear && $month == $curMonth){
  96. $timeAgo = $curDayNum - $dayNum;
  97. if($timeAgo > 29){
  98. echo "Sent about a month ago";
  99. }else if($timeAgo == 1){
  100. echo "Sent a day ago";
  101. }else{
  102. echo "Sent " . $timeAgo . " days ago";
  103. }
  104. }else if($year == $curYear){
  105. $timeAgo = $curMonth - $month;
  106. if($timeAgo == 1){
  107. echo "Sent one month ago";
  108. }else{
  109. echo "Sent " . $timeAgo . " months ago";
  110. }
  111. }else{
  112. $timeAgo = $curYear - $year;
  113. if($timeAgo == 1){
  114. echo "Sent " . $timeAgo . " year ago";
  115. }else{
  116. echo "Sent " . $timeAgo . " years ago";
  117. }
  118. }
  119. // if it's a reply, append a "In reply to at the end"
  120. if($tweets[$i]['in_reply_to_screen_name'] !== NULL){
  121. echo " in reply to <a href="http://twitter.com/" . $tweets[$i]['in_reply_to_screen_name'] . "">" . $tweets[$i]['in_reply_to_screen_name'] . "</a>.";
  122. }else{
  123. echo ".";
  124. }
  125. echo "</li>";
  126. }
  127. echo "</ul>";
  128. echo "<p><a href="http://twitter.com/$username">Follow Me</a></p>";
  129. }
  130. ?>

以上是关于Twitter PHP脚本的主要内容,如果未能解决你的问题,请参考以下文章

Twitter PHP脚本

Twitter自动跟踪PHP脚本

PHP Twitter跟随脚本修改

来自fabric的Twitter登录在片段中不起作用

PHP - Twitter API - 使用空间时请求损坏

PHP 用于Twitter搜索的SimpleXML代码