/
twitter_roll.php
130 lines (109 loc) · 3.92 KB
/
twitter_roll.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
require('OAuth.php');
require('TwitterOAuth.php');
class TwitterListMembers
{
// The following four tokens can usually be found within http://dev.twitter.com/apps
const CONSUMER_KEY = ''; // your Twitter app consumer key
const CONSUMER_SECRET = ''; // you Twitter app consumer secret
const OAUTH_TOKEN = ''; // your Twitter OAuth token for this app
const OAUTH_SECRET = ''; // your Twitter OAuth secret for this app
const TWITTER_LIST = 'yourtwittername/yourtwitterlist'; // Twitter list to display
public function get($list)
{
// Unfortunately, the GET LIST MEMBERS API requires authentication for
// some reason, even when the same data is available on the website. So
// this requires hard-coding (or otherwise storing) a user's OAuth
// credentials in here. Be sure that the application has read-only
// permissions to reduce the chance of tomfoolery.
$oauth = new TwitterOAuth(self::CONSUMER_KEY, self::CONSUMER_SECRET, self::OAUTH_TOKEN, self::OAUTH_SECRET);
$response = $oauth->OAuthRequest("http://api.twitter.com/1/$list/members.json", array(), 'GET');
$members = json_decode($response);
usort($members->users, array(self, 'lastUpdated'));
return $members->users;
}
public static function lastUpdated($member1, $member2)
{
$lastStatus1 = strtotime($member1->status->created_at);
$lastStatus2 = strtotime($member2->status->created_at);
return ($lastStatus1 > $lastStatus2) ? -1 : 1;
}
public static function howLongAgo($then)
{
$delta = time() - $then;
if ($delta < 60)
{
$howlong = 'less than a minute ago';
}
else if ($delta < 120)
{
$howlong = 'about a minute ago';
}
else if ($delta < (60 * 60))
{
$howlong = round((float)($delta / 60.0)) . ' minutes ago';
}
else if ($delta < (120 * 60))
{
$howlong = 'about an hour ago';
}
else if ($delta < (24 * 60 * 60))
{
$howlong = 'about ' . round((float) ($delta / 3600.0)) . ' hours ago';
}
else if ($delta < (48 * 60 * 60))
{
$howlong = '1 day ago';
}
else
{
$howlong = round((float) ($delta / 86400.0)) . ' days ago';
}
return $howlong;
}
public function render($members)
{
foreach ((array) $members as $member)
{
$howLongAgo = self::howLongAgo(strtotime($member->status->created_at));
echo <<<EOF
<div id="twitter_roll" style="font-family: Arial; font-size: 8pt; width: 50%;">
<h3>{$member->name} ({$member->screen_name})</h3>
<img src="{$member->profile_image_url}" style="float: left; margin-right: 10px;">
<p>{$member->description}</p>
EOF;
$status = "<span> {$member->status->text} </span> <a style=\"font-size: 85%;\" href=\"http://twitter.com/{$member->screen_name}/statuses/{$member->status->id}\"> $howLongAgo </a>";
if ($member->protected)
{
$status = "<em> This person has protected their tweets. </em>";
}
echo <<<EOF
<ul style="margin-left: 50px;">
<li> $status </li>
</ul>
<p> <a href="http://twitter.com/{$member->screen_name}" rel="me"> Follow {$member->screen_name} on Twitter</a> </p>
</div>
EOF;
}
}
}
?>
<html>
<body>
<h2>Welcome to the Twitter roll!</h2>
<p>
<?php
$list = new TwitterListMembers();
try
{
$members = $list->get(TwitterListMembers::TWITTER_LIST);
$list->render($members);
}
catch (Exception $e)
{
echo "<pre>Twitter API error. No Twitter roll for you!</pre>";
}
?>
</p>
</body>
</html>