Skip to content

Instantly share code, notes, and snippets.

@mckabi
Last active September 27, 2015 00:27
Show Gist options
  • Save mckabi/1182885 to your computer and use it in GitHub Desktop.
Save mckabi/1182885 to your computer and use it in GitHub Desktop.
Get all of RTers by tweet
<?php
/*
* 공식 트위터 사이트에서는 트윗을 RT한 사람 목록을 볼 수가 있는데
* 숫자가 조금만 많아도 줄여서 보여주기 때문에 모두 확인하기가 어렵다.
*
* 트위터 API를 써서 모두 확인하는 방법
*/
$tweet_id = '101450759463383041'; // 트위터 사이트에서 트윗한 시간을 누르면 확인 가능
// https://dev.twitter.com/docs/api/1/get/statuses/%3Aid/retweeted_by
$url = "http://api.twitter.com/1/statuses/$tweet_id/retweeted_by.json";
$page = 1;
$sum = 0;
$RTed_by = array();
do {
$r = curl_get($url, array('count' => 100, 'page' => $page));
$d = json_decode($r);
echo "Page $page : ".count($d)."\n";
$page += 1;
$sum += count($d);
$RTed_by = array_merge($RTed_by, $d);
} while (count($d) > 0);
echo "Total : $sum\n";
// RTed_by 배열이 RT한 사람의 계정 정보를 모두 담고 있다.
// screen name만 뽑아서 보기:
print_r(array_map(create_function('$u', 'return $u->screen_name;'), $RTed_by));
// 지정한 URL에 접근해서 결과를 가져오는 유틸리티 함수
// http://kr.php.net/manual/en/function.curl-exec.php#98628 에서 가져옴
function curl_get($url, array $get=array(), array $options=array()) {
$defaults = array(
CURLOPT_URL => $url. (strpos($url, '?') === FALSE ? '?' : ''). http_build_query($get),
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 4,
);
$ch = curl_init();
curl_setopt_array($ch, ($options + $defaults));
if (!$result = curl_exec($ch)) trigger_error(curl_error($ch));
curl_close($ch);
return $result;
}
Page 1 : 92
Page 2 : 98
Page 3 : 97
Page 4 : 6
Page 5 : 0
Total : 293
Array
(
[0] => ***twitter id #1***
[1] => ***twitter id #2***
...snip...
[291] => ***twitter id #292***
[292] => ***twitter id #293***
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment