我正在使用它https://api.twitter.com/1.1/followers/ids.json?cursor=-1&screen_name=sitestreams&count=5000
来列出 Twitter 关注者列表,但我只得到了 200 个关注者的列表。如何使用新的 API 1.1 增加 Twitter 关注者列表?
3 回答
您必须首先设置您的应用程序
<?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
这是我在我的平台上运行/更新完整的追随者 ID 列表的方式。我会避免使用sleep()
@aphoe 之类的脚本。长时间保持连接打开真的很糟糕 - 如果您的用户有 1MILL 关注者会发生什么?你打算保持这个连接打开一个星期吗?大声笑如果必须,运行 cron 或保存到 redis/memcache。冲洗并重复,直到你得到所有的追随者。
请注意,我下面的代码是一个每分钟通过 cron 命令运行的类。我正在使用 Laravel 5.1。所以你可能会忽略很多这样的代码,因为它是我平台独有的。例如TwitterOAuth
(获取我在 db 上拥有的所有 oAuth),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;
}
}
});
}
您一次不能获取超过 200 个......在 count 的文档中明确说明:
每页返回的用户数,最多为 200。默认为 20。
你可以通过分页使用
"cursor=-1" #表示第1页,"如果没有提供光标,则假定值为-1,即第一页。"