/ 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.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php // the number of tweets we want to display $tweetsToDisplay = 10; $username = "username"; // make sure you change this to your username $twitterrequest = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=' . $username . '&count=' . $tweetsToDisplay; // parameter 'true' is necessary for output as PHP array echo "<h3><a href=\"http://twitter.com/$username\">Twitter Updates</a></h3>"; echo "Twitter doesn't seem to be working at the moment."; echo "There was an issue with Twitter."; }else{ // Twitter uses the GMT timezone // This is Twitter's response date format // then we explode it to use it later // turn the month name into a number $curMonthNum = array("Filler","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"); for($cm=1;$cm<=12;$cm++){ if($curMonthNum[$cm] == $curMonth){ $curMonth = $cm; } } echo "<ul>"; // a quick bug fix, without this extra bullets may be in the list if($tweets[0]['user']['statuses_count'] < $tweetsToDisplay){ $display = $tweets[0]['user']['statuses_count'] - 1; }else{ $display = $tweetsToDisplay - 1; } for($i=0;$i<=$display;$i++){ echo "<li>"; // we are going to check for any links, @users, or #hashtags $tweetText = $tweets[$i]['text'] . "<br />"; // if there is, replace that match with an actual link $tweetText = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@','<a href="$1">$1</a>',$tweetText); } // check to see if it's a reply } // then check for a hash tag $tweetText = preg_replace('/\B#(\w*[a-zA-Z]+\w*)/', '<a href="http://twitter.com/search?q=%23$1">#$1</a>',$tweetText); } // Finally, we can echo the Tweet echo $tweetText; // then we figure out the time ago it was sent $tweetTime = $tweets[$i]['created_at']; // Because the month comes in as a string, we need to covert it to a integer $monthNum = array("Filler","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"); for($m=1;$m<=12;$m++){ if($monthNum[$m] == $month){ $month = $m; } } // and then echo it // if the month, day, and hour are the same, echo the minutes ago if($year == $curYear && $month == $curMonth && $dayNum == $curDayNum && $timeArray[0] == $curTimeArray[0]){ $timeAgo = $curTimeArray[1] - $timeArray[1]; if($timeAgo == 0){ echo "Sent less then a minute ago"; }else if($timeAgo == 1){ echo "Sent one minute ago"; }else if($timeAgo > 50){ echo "Sent about an hour ago"; }else{ echo "Sent " . $timeAgo . " minutes ago"; } // if it was less then a day ago, echo the hours ago }else if($year == $curYear && $month == $curMonth && $dayNum == $curDayNum){ $timeAgo = $curTimeArray[0] - $timeArray[0]; if($timeAgo > 22){ echo "Sent about a day ago"; }else if($timeAgo ==1){ echo "Sent an hour ago"; }else{ echo "Sent " . $timeAgo . " hours ago"; } }else if($year == $curYear && $month == $curMonth){ $timeAgo = $curDayNum - $dayNum; if($timeAgo > 29){ echo "Sent about a month ago"; }else if($timeAgo == 1){ echo "Sent a day ago"; }else{ echo "Sent " . $timeAgo . " days ago"; } }else if($year == $curYear){ $timeAgo = $curMonth - $month; if($timeAgo == 1){ echo "Sent one month ago"; }else{ echo "Sent " . $timeAgo . " months ago"; } }else{ $timeAgo = $curYear - $year; if($timeAgo == 1){ echo "Sent " . $timeAgo . " year ago"; }else{ echo "Sent " . $timeAgo . " years ago"; } } // if it's a reply, append a "In reply to at the end" if($tweets[$i]['in_reply_to_screen_name'] !== NULL){ echo " in reply to <a href=\"http://twitter.com/" . $tweets[$i]['in_reply_to_screen_name'] . "\">" . $tweets[$i]['in_reply_to_screen_name'] . "</a>."; }else{ echo "."; } echo "</li>"; } echo "</ul>"; echo "<p><a href=\"http://twitter.com/$username\">Follow Me</a></p>"; } ?>