Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function twitter($tweets,$account) {
- // Check the current twitter API limit for the connected user IP
- $twitterlimit = file_get_contents("http://api.twitter.com/1/account/rate_limit_status.json", true);
- $hourlylimit = json_decode($twitterlimit, true);
- $limit = $hourlylimit['remaining_hits'];
- // If API limit is 50 or below use local json file instead of calling the API. Do this until the API limit refreshes and the limit goes above 50.
- if ($limit <= 50) :
- $json = file_get_contents("tweets.json", true);
- $tweet = json_decode($json, true);
- else :
- // If API limit is above 50 call the API and get the tweets. Also store the tweets in a local file called tweets.json for use when API limit gets low.
- $json = file_get_contents("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=".$account."&include_rts=true"."&count=".$tweets."", true);
- $tweet = json_decode($json, true);
- $fp = fopen('tweets.json', 'w');
- fwrite($fp, $json);
- fclose($fp);
- endif;
- // Count the number of tweets
- $count = count($tweet);
- // Loop through each tweet and echo it to the screen
- for($i=0;$i<$count;$i++){
- echo '<div class="tweet">'.'<div class="twitter-text">'.formatTweet($tweet[$i]['text']).'</div>'.'<div class="twitter-time"><a href="http://twitter.com/'.$account.'/status/'.formatTweet($tweet[$i]['id_str']).'" target="_blank">'.get_elapsedtime(strtotime($tweet[$i]['created_at'])).'</a></div>'.'</div>';
- }
- }
- // Helper funtion to the twitter function. This fixes the hyperlinks in the tweets so the URL's work
- function formatTweet($tweet) {
- $tweet = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $tweet);
- $tweet = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $tweet);
- $tweet = preg_replace("/@(\w+)/", "<a href=\"http://twitter.com/\\1\" target=\"_blank\">@\\1</a>", $tweet);
- $tweet = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $tweet);
- return $tweet;
- }
- // Helper function to the twitter function. This gets the elapsed time since the tweet was posted
- function get_elapsedtime($time)
- {
- $gap = time() - $time;
- if ($gap < 5) {
- return 'less than 5 seconds ago';
- } else if ($gap < 10) {
- return 'less than 10 seconds ago';
- } else if ($gap < 20) {
- return 'less than 20 seconds ago';
- } else if ($gap < 40) {
- return 'half a minute ago';
- } else if ($gap < 60) {
- return 'less than a minute ago';
- }
- $gap = round($gap / 60);
- if ($gap < 60) {
- return $gap.' minute'.($gap > 1 ? 's' : '').' ago';
- }
- $gap = round($gap / 60);
- if ($gap < 24) {
- return 'about '.$gap.' hour'.($gap > 1 ? 's' : '').' ago';
- }
- return date('H:i M jS, Y', $time);
- }
- ?>
- <?php twitter($_GET['tweets'],$_GET['account']); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement