This repository has been archived by the owner on Jun 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweet_ids.php
62 lines (48 loc) Β· 1.62 KB
/
tweet_ids.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
<?php
require 'bootstrap.php';
use Abraham\TwitterOAuth\TwitterOAuth;
/* Get user access tokens out of the session. */
$access_token = $_SESSION['access_token'];
/* Create a TwitterOauth object with consumer/user tokens. */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
/* If method is set change API call made. Test is called by default. */
$user = $connection->get('account/verify_credentials', ['tweet_mode' => 'extended', 'include_entities' => 'true']);
function get_id_str($status) {
return $status->id_str;
}
$tweets = [];
// max count (amount of tweets you get in one API call) is 200
$options = [
'count' => 200,
'trim_user' => TRUE,
];
while (TRUE) {
if(isset($max_id)) {
$options['max_id'] = $max_id;
}
// statuses/user_timeline is tweets and retweets
// favorites/list is favorites
$response_data = $connection->get('statuses/user_timeline', $options);
if (is_object($response_data) && $response_data->errors) {
break;
}
$ids = array_map("get_id_str", $response_data);
if(isset($max_id)) {
// remove first element as it is also the last of the former call
array_shift($ids);
}
$tweets = array_merge($tweets, $ids);
if(count($ids) > 0) {
$max_id = $ids[count($ids) - 1];
} else {
break;
}
// uncomment for testing to save API quota:
//break;
}
header('Content-Type: application/json');
if (is_object($response_data) && $response_data->errors) {
echo json_encode($response_data);
} else {
echo json_encode($tweets);
}