-
Notifications
You must be signed in to change notification settings - Fork 335
/
friends.php
95 lines (81 loc) · 2.62 KB
/
friends.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
<?php
/**
* Retrieve a list of friends for the authenticating user and then lookup
* their details using users/lookup. If you want to retrieve followers you
* can change the URL from '1/friends/ids' to '1/followers/ids'.
*
* Although this example uses your user token/secret, you can use
* the user token/secret of any user who has authorised your application.
*
* Instructions:
* 1) If you don't have one already, create a Twitter application on
* https://dev.twitter.com/apps
* 2) From the application details page copy the consumer key and consumer
* secret into the place in this code marked with (YOUR_CONSUMER_KEY
* and YOUR_CONSUMER_SECRET)
* 3) From the application details page copy the access token and access token
* secret into the place in this code marked with (A_USER_TOKEN
* and A_USER_SECRET)
* 4) Visit this page using your web browser.
*
* @author themattharris
*/
define('LOOKUP_SIZE', 100);
require '../tmhOAuth.php';
require '../tmhUtilities.php';
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => 'YOUR_CONSUMER_KEY',
'consumer_secret' => 'YOUR_CONSUMER_SECRET',
'user_token' => 'A_USER_TOKEN',
'user_secret' => 'A_USER_SECRET',
));
function check_rate_limit($response) {
$headers = $response['headers'];
if ($headers['x_ratelimit_remaining'] == 0) :
$reset = $headers['x_ratelimit_reset'];
$sleep = time() - $reset;
echo 'rate limited. reset time is ' . $reset . PHP_EOL;
echo 'sleeping for ' . $sleep . ' seconds';
sleep($sleep);
endif;
}
$cursor = '-1';
$ids = array();
while (true) :
if ($cursor == '0')
break;
$tmhOAuth->request('GET', $tmhOAuth->url('1/friends/ids'), array(
'cursor' => $cursor
));
// check the rate limit
check_rate_limit($tmhOAuth->response);
if ($tmhOAuth->response['code'] == 200) {
$data = json_decode($tmhOAuth->response['response'], true);
$ids = array_merge($ids, $data['ids']);
$cursor = $data['next_cursor_str'];
} else {
echo $tmhOAuth->response['response'];
break;
}
usleep(500000);
endwhile;
// lookup users
$paging = ceil(count($ids) / LOOKUP_SIZE);
$users = array();
for ($i=0; $i < $paging ; $i++) {
$set = array_slice($ids, $i*LOOKUP_SIZE, LOOKUP_SIZE);
$tmhOAuth->request('GET', $tmhOAuth->url('1/users/lookup'), array(
'user_id' => implode(',', $set)
));
// check the rate limit
check_rate_limit($tmhOAuth->response);
if ($tmhOAuth->response['code'] == 200) {
$data = json_decode($tmhOAuth->response['response'], true);
$users = array_merge($users, $data);
} else {
echo $tmhOAuth->response['response'];
break;
}
}
var_dump($users);
?>