* @license http://opensource.org/licenses/gpl-license.php GNU Public License * @link http://github.com/kmaida/twitter-timeline-php * @credits Thank you to for base for "time ago" calculations * **/ ############################################################### ## SETTINGS // Set access tokens $settings = array( 'consumer_key' => "BbKsuZQ4HkpQpmQhuxPwpLxVW", 'consumer_secret' => "JhHo0x9D5nx89MI8jVsySvrJH8Vbbf7EgLoJPYYzAHpgGDOG5f", 'oauth_access_token' => "1018413731510771713-C7XwvicqmkyKBy8kf7TGagYXPgT0vP", 'oauth_access_token_secret' => "peQth0MMGURIw86b7s8y2efo8HOqCquRP0LfgOXWypfRN" ); // Set API request URL and timeline variables if needed $url = 'https://api.twitter.com/1.1/statuses/user_timeline.json'; $twitterUsername = "WinsDominoes"; $tweetCount = 500; // Use private tokens for development if they exist; delete when no longer necessary $tokens = '_utils/tokens.php'; is_file($tokens) AND include $tokens; // Require the OAuth class require_once('twitter-api-oauth.php'); ############################################################### ## MAKE GET REQUEST $getfield = '?screen_name=' . $twitterUsername . '&count=' . $tweetCount; $twitter = new TwitterAPITimeline($settings); $json = $twitter->setGetfield($getfield) // Note: Set the GET field BEFORE calling buildOauth() ->buildOauth($url, $requestMethod) ->performRequest(); $twitter_data = json_decode($json, true); // Create an array with the fetched JSON data ############################################################### ## DO SOMETHING WITH THE DATA //-------------------------------------------------------------- Format the time(ago) and date of each tweet function timeAgo($dateStr) { $timestamp = strtotime($dateStr); $day = 60 * 60 * 24; $today = time(); // current unix time $since = $today - $timestamp; # If it's been less than 1 day since the tweet was posted, figure out how long ago in seconds/minutes/hours if (($since / $day) < 1) { $timeUnits = array( array(60 * 60, 'h'), array(60, 'm'), array(1, 's') ); for ($i = 0, $n = count($timeUnits); $i < $n; $i++) { $seconds = $timeUnits[$i][0]; $unit = $timeUnits[$i][1]; if (($count = floor($since / $seconds)) != 0) { break; } } return "$count{$unit}"; # If it's been a day or more, return the date: day (without leading 0) and 3-letter month } else { return date('j M', strtotime($dateStr)); } } //-------------------------------------------------------------- Format the tweet text (links, hashtags, mentions) function formatTweet($tweet) { $linkified = '@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@'; $hashified = '/(^|[\n\s])#([^\s"\t\n\r<:]*)/is'; $mentionified = '/(^|[\n\s])@([^\s"\t\n\r<:]*)/is'; $prettyTweet = preg_replace( array( $linkified, $hashified, $mentionified ), array( '$1', '$1#$2', '$1@$2' ), $tweet ); return $prettyTweet; } //-------------------------------------------------------------- Timeline HTML output # This output markup adheres to the Twitter developer display requirements (https://dev.twitter.com/terms/display-requirements) # Open the timeline list echo '
    '; # The tweets loop foreach ($twitter_data as $tweet) { $retweet = $tweet['retweeted_status']; $isRetweet = !empty($retweet); # Retweet - get the retweeter's name and screen name $retweetingUser = $isRetweet ? $tweet['user']['name'] : null; $retweetingUserScreenName = $isRetweet ? $tweet['user']['screen_name'] : null; # Tweet source user (could be a retweeted user and not the owner of the timeline) $user = !$isRetweet ? $tweet['user'] : $retweet['user']; $userName = $user['name']; $userScreenName = $user['screen_name']; $userAvatarURL = stripcslashes($user['profile_image_url']); $userAccountURL = 'http://twitter.com/' . $userScreenName; # The tweet $id = number_format($tweet['id'], 0, '.', ''); $formattedTweet = !$isRetweet ? formatTweet($tweet['text']) : formatTweet($retweet['text']); $statusURL = 'http://twitter.com/' . $userScreenName . '/status/' . $id; $date = timeAgo($tweet['created_at']); # Reply $replyID = number_format($tweet['in_reply_to_status_id'], 0, '.', ''); $isReply = !empty($replyID); # Tweet actions (uses web intents) $replyURL = 'https://twitter.com/intent/tweet?in_reply_to=' . $id; $retweetURL = 'https://twitter.com/intent/retweet?tweet_id=' . $id; $favoriteURL = 'https://twitter.com/intent/favorite?tweet_id=' . $id; ?>
  • ' . $formattedTweet . '

    '; echo '

    '; if ($isReply) { echo ' In reply to... '; } if ($isRetweet) { echo ' Retweeted by ' . $retweetingUser . ' '; } echo 'Details

    '; ?>
  • '; # echo $json; // Uncomment this line to view the entire JSON array. Helpful: http://www.freeformatter.com/json-formatter.html ?>