[英]How to get full list of Twitter followers using new API 1.1
我正在使用这个https://api.twitter.com/1.1/followers/ids.json?cursor=-1&screen_name=sitestreams&count=5000
列出Twitter粉丝列表,但我只列出了200个关注者。 如何使用新的API 1.1增加Twitter粉丝列表?
您必须先设置应用程序
<?php
$consumerKey = 'Consumer-Key';
$consumerSecret = 'Consumer-Secret';
$oAuthToken = 'OAuthToken';
$oAuthSecret = 'OAuth Secret';
# API OAuth
require_once('twitteroauth.php');
$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $oAuthToken, $oAuthSecret);
你可以从这里下载twitteroauth.php: https : //github.com/elpeter/pv-auto-tweets/blob/master/twitteroauth.php
然后
您可以像这样检索您的粉丝:
$tweet->get('followers/ids', array('screen_name' => 'YOUR-SCREEN-NAME-USER'));
如果要检索下一组5000个关注者,则必须在第一次调用时添加游标值。
$tweet->get('followers/ids', array('screen_name' => 'YOUR-SCREEN-NAME-USER', 'cursor' => 9999999999));
您可以阅读:使用游标在此链接中导航集合: https : //dev.twitter.com/docs/misc/cursoring
您不能一次获取超过200个...在文档中明确说明了count:
每页返回的用户数,最多为200.默认为20。
你可以通过分页使用它
“cursor = -1”#means page 1,“如果没有提供光标,则假设值为-1,这是第一个”页面“。
以下是我在平台上运行/更新完整的关注者ID列表的方法。 我会避免像@aphoe脚本那样使用sleep()
。 保持连接打开的时间真的很糟糕 - 如果您的用户有1MILL粉丝会怎么样? 你打算将这个连接打开一个星期? 大声笑如果必须,运行cron或保存到redis / memcache。 冲洗并重复,直到获得所有粉丝。
注意,我下面的代码是一个每分钟运行一次cron命令的类。 我正在使用Laravel 5.1。 所以你可能会忽略很多这样的代码,因为它对我的平台来说是独一无二的。 比如TwitterOAuth
(它获取我在db上的所有oAuths), TwitterFollowerList
是另一个表,我检查条目是否已经存在, TwitterFollowersDaily
是另一个表,我存储/更新当天用户的总金额,而TwitterApi
是Abraham\\TwitterOAuth
包。 你可以使用任何库。
这可能会让您很好地理解您可能会做同样的事情,甚至可以找到更好的方法。 我不会解释所有的代码,因为有很多事情发生,但你应该能够引导它。 如果您有任何疑问,请告诉我。
/**
* Update follower list for each oAuth
*
* @return response
*/
public function updateFollowers()
{
TwitterOAuth::chunk(200, function ($oauths)
{
foreach ($oauths as $oauth)
{
$page_id = $oauth->page_id;
$follower_list = TwitterFollowerList::where('page_id', $page_id)->first();
if (!$follower_list || $follower_list->updated_at < Carbon::now()->subMinutes(15))
{
$next_cursor = isset($follower_list->next_cursor) ? $follower_list->next_cursor : -1;
$ids = isset($follower_list->follower_ids) ? $follower_list->follower_ids : [];
$twitter = new TwitterApi($oauth->oauth_token, $oauth->oauth_token_secret);
$results = $twitter->get("followers/ids", ["user_id" => $page_id, "cursor" => $next_cursor]);
if (isset($results->errors)) continue;
$ids = $results->ids;
if ($results->next_cursor !== 0)
{
$ticks = 0;
do
{
if ($ticks === 13)
{
$ticks = 0;
break;
}
$ticks++;
$results = $twitter->get("followers/ids", ["user_id" => $page_id, "cursor" => $results->next_cursor]);
if (!$results) break;
$more_ids = $results->ids;
$ids = array_merge($ids, $more_ids);
}
while ($results->next_cursor > 0);
}
$stats = [
'page_id' => $page_id,
'follower_count' => count($ids),
'follower_ids' => $ids,
'next_cursor' => ($results->next_cursor > 0) ? $results->next_cursor : null,
'updated_at' => Carbon::now()
];
TwitterFollowerList::updateOrCreate(['page_id' => $page_id], $stats);
TwitterFollowersDaily::updateOrCreate([
'page_id' => $page_id,
'date' => Carbon::now()->toDateString()
],
[
'page_id' => $page_id,
'date' => Carbon::now()->toDateString(),
'follower_count' => count($ids),
]
);
continue;
}
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.