Skip to content

Instantly share code, notes, and snippets.

@rakuishi
Created January 17, 2015 01:52
Show Gist options
  • Save rakuishi/2170cfecef90382bbf6f to your computer and use it in GitHub Desktop.
Save rakuishi/2170cfecef90382bbf6f to your computer and use it in GitHub Desktop.
Show Twitter friend's last post datetime
<?php
require_once(dirname(__file__) . "/twitteroauth/twitteroauth.php");
date_default_timezone_set('Asia/Tokyo');
define('TWITTER_CONSUMER_KEY', '***');
define('TWITTER_CONSUMER_SECRET', '***');
define('TWITTER_ACCESS_TOKEN', '***');
define('TWITTER_ACCESS_TOKEN_SECRET', '***');
// https://dev.twitter.com/rest/reference/get/friends/list
function requestFriendsList($twitterObject, $cursor) {
$params = array(
'screen_name' => 'rakuishi07',
'count' => '200',
'include_user_entities' => 'false',
'cursor' => (is_null($cursor)) ? '-1' : $cursor,
);
return $twitterObject->OAuthRequest('https://api.twitter.com/1.1/friends/list.json', 'GET', $params);
}
function getTwitterFriends() {
echo "Loading...\n";
$twitterObject = new TwitterOAuth(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, TWITTER_ACCESS_TOKEN, TWITTER_ACCESS_TOKEN_SECRET);
$cursor = -1;
$friends = array();
do {
$request = requestFriendsList($twitterObject, $cursor);
$json = json_decode($request);
$cursor = $json->next_cursor;
$users = $json->users;
foreach ($users as $user) {
$date = DateTime::createFromFormat('D M j H:i:s P Y', $user->status->created_at);
$friends[] = array(
'screen_name' => $user->screen_name,
'created_at' => $date->getTimestamp(),
);
}
echo count($friends) . "\n";
} while (!is_null($cursor) && $cursor != 0);
$keys = array();
foreach ($friends as $key => $value){
$keys[$key] = $value['created_at'];
}
array_multisort($keys , SORT_ASC , $friends);
$now = time();
foreach ($friends as $friend) {
$day = ($now - $friend['created_at']) / 86400;
echo $friend['screen_name'] . ' ' . $day . "\n";
}
}
getTwitterFriends();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment