Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cecilemuller
Created January 23, 2012 10:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cecilemuller/1662287 to your computer and use it in GitHub Desktop.
Save cecilemuller/1662287 to your computer and use it in GitHub Desktop.
Get a user's most recent tweets in PHP (including retweets, formatted mentions/hashtags and friendly dates)
<?php
/**
* Calculate the time elapsed from the timestamp described in the string
* and converts into a human-readable version (e.g. "about 3 days ago").
*/
function how_long_ago($date) {
$ago = false;
$elapsed = time() - strtotime($date);
if ($elapsed <= 1) {
$ago = 'less than 1 second ago';
} else if ($elapsed < 60) {
$ago = $elapsed . ' seconds ago';
} else if ($elapsed < 3600) {
$minutes = round($elapsed / 60);
$ago = 'about ' . $minutes . ' minute' . ($minutes > 1 ? 's' : '') . ' ago';
} else if ($elapsed < 57600) {
$hours = round($elapsed / 3600);
$ago = 'about ' . $hours . ' hour' . ($hours > 1 ? 's' : '') . ' ago';
} else if ($elapsed < (time() - strtotime('yesterday'))) {
$ago = 'yesterday';
} else if ($elapsed < 86400) {
$hours = round($elapsed / 3600);
$ago = 'about ' . $hours . ' hour' . ($hours > 1 ? 's' : '') . ' ago';
} else if ($elapsed < 561600) {
$ago = 'about ' . round($elapsed / 86400) . ' days ago';
} else if ($elapsed < (time() - strtotime('last week'))) {
$ago = 'last week';
} else if (round($elapsed / 604800) == 1) {
$ago = 'about a week ago';
} else if ($elapsed < 2116800) {
$ago = 'about ' . round($elapsed / 604800) . ' weeks ago';
} else if ($elapsed < (time() - strtotime('last month'))) {
$ago = 'last month';
} else if (round($elapsed / 2419200) == 1) {
$ago = 'about a month ago';
} else if ($elapsed < 27820800) {
$ago = 'about ' . round($elapsed / 2419200) . ' months ago';
} else if ($elapsed < (time() - strtotime('last year'))) {
$ago = 'last year';
} else if (round($elapsed / 31449600) == 1) {
$ago = 'about a year ago';
} else if ($elapsed >= 29030400) {
$ago = 'about ' . round($elapsed / 31449600) . ' years ago';
}
return $ago;
}
/**
* Adds links to entities (mentions, hashtags and urls) in a tweet text.
*/
function entities($text, $entities) {
$search = array();
$replace = array();
foreach($entities->user_mentions as $entity) {
$search[] = '@' . $entity->screen_name;
$replace[] = '<a title="' . $entity->name . ' on Twitter (@' . $entity->screen_name . ')" target="_blank" href="http://twitter.com/' . $entity->screen_name . '">@' . $entity->screen_name . '</a>';
}
foreach($entities->hashtags as $entity){
$search[] = '#' . $entity->text;
$replace[] = '<a title="#' . $entity->text . ' on Twitter" target="_blank" href="http://twitter.com/search?q=%23' . $entity->text . '">#' . $entity->text . '</a>';
}
foreach($entities->urls as $entity){
$search[] = $entity->url;
$replace[] = '<a title="' . $entity->expanded_url . '" target="_blank" href="' . $entity->url . '">' . (isset($entity->display_url) ? $entity->display_url : $entity->expanded_url) . '</a>';
}
return str_replace($search, $replace, $text);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=wildpeaks&count=20');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);
curl_close($ch);
$tweets = @json_decode($json);
if (!empty($tweets)) {
foreach ($tweets as $tweet) {
echo '<div class="tweet">';
$retweeted = false;
if (isset($tweet->retweeted_status)) {
$retweeted = $tweet->user;
$tweet = $tweet->retweeted_status;
}
$user = $tweet->user;
echo '<div class="author">';
if (isset($user->profile_image_url)) echo '<img src="' . $user->profile_image_url . '" /> ';
if ($retweeted) echo 'RT ';
echo '<a lang="en" href="http://twitter.com/' . $user->screen_name . '" title="' . $user->description . ' - ' . $user->name . ' on Twitter (@' . $user->screen_name . ')">' . $user->screen_name . '</a>';
echo ' &ndash; ' . how_long_ago($tweet->created_at);
echo '</div>';
echo '<p>' . entities($tweet->text, $tweet->entities) . '</p>';
echo '</div>';
}
}
?>
* {
margin: 0;
padding: 0;
font-smooth: always;
}
html, body {
margin: 0;
padding: 0;
border: 0;
height: 100%;
}
body {
color: #7F7F7F;
background: #FFFFFF;
font-size: 62.5%;
font-family: sans-serif;
}
.tweet {
font-size: 1.2em;
line-height: 2.4em;
border: 1px solid #C0C0C0;
padding: 20px;
margin: 10px;
}
.tweet img {
height: 24px;
vertical-align: middle;
margin-right: 2px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment