Want more features on Pastebin? Sign Up, it's FREE!
Guest

PHP Twitter API Function

By: a guest on Jul 27th, 2011  |  syntax: PHP  |  size: 2.92 KB  |  views: 223  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. function twitter($tweets,$account) {
  3.         // Check the current twitter API limit for the connected user IP
  4.         $twitterlimit = file_get_contents("http://api.twitter.com/1/account/rate_limit_status.json", true);
  5.         $hourlylimit = json_decode($twitterlimit, true);
  6.         $limit = $hourlylimit['remaining_hits'];
  7.        
  8.         // 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.
  9.         if ($limit <= 50) :
  10.                 $json = file_get_contents("tweets.json", true);
  11.                 $tweet = json_decode($json, true);
  12.         else :
  13.                 // 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.
  14.                 $json = file_get_contents("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=".$account."&include_rts=true"."&count=".$tweets."", true);
  15.                 $tweet = json_decode($json, true);
  16.                 $fp = fopen('tweets.json', 'w');
  17.                 fwrite($fp, $json);
  18.                 fclose($fp);
  19.         endif;
  20.        
  21.         // Count the number of tweets
  22.         $count = count($tweet);
  23.        
  24.         // Loop through each tweet and echo it to the screen
  25.         for($i=0;$i<$count;$i++){
  26.                 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>';
  27.         }
  28. }
  29.  
  30. // Helper funtion to the twitter function. This fixes the hyperlinks in the tweets so the URL's work
  31. function formatTweet($tweet) {
  32.         $tweet = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $tweet);
  33.         $tweet = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $tweet);
  34.         $tweet = preg_replace("/@(\w+)/", "<a href=\"http://twitter.com/\\1\" target=\"_blank\">@\\1</a>", $tweet);
  35.         $tweet = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $tweet);
  36.        
  37.         return $tweet;
  38. }
  39.  
  40. // Helper function to the twitter function. This gets the elapsed time since the tweet was posted
  41. function get_elapsedtime($time)
  42. {
  43.     $gap = time() - $time;
  44.     if ($gap < 5) {
  45.         return 'less than 5 seconds ago';
  46.     } else if ($gap < 10) {
  47.         return 'less than 10 seconds ago';
  48.     } else if ($gap < 20) {
  49.         return 'less than 20 seconds ago';
  50.     } else if ($gap < 40) {
  51.         return 'half a minute ago';
  52.     } else if ($gap < 60) {
  53.         return 'less than a minute ago';
  54.     }
  55.     $gap = round($gap / 60);
  56.     if ($gap < 60)  {
  57.         return $gap.' minute'.($gap > 1 ? 's' : '').' ago';
  58.     }
  59.     $gap = round($gap / 60);
  60.     if ($gap < 24)  {
  61.         return 'about '.$gap.' hour'.($gap > 1 ? 's' : '').' ago';
  62.     }
  63.     return date('H:i M jS, Y', $time);
  64. }
  65. ?>
  66. <?php twitter($_GET['tweets'],$_GET['account']); ?>
clone this paste RAW Paste Data