首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用新的API 1.1获取Twitter追随者的完整列表

如何使用新的API 1.1获取Twitter追随者的完整列表
EN

Stack Overflow用户
提问于 2013-07-02 12:25:31
回答 3查看 21.3K关注 0票数 4

我正在使用这个https://api.twitter.com/1.1/followers/ids.json?cursor=-1&screen_name=sitestreams&count=5000列出Twitter的关注者列表,但是我只得到了200个关注者的列表。如何使用新的API 1.1增加Twitter追随者列表?

EN

回答 3

Stack Overflow用户

发布于 2013-08-08 00:27:03

您必须首先设置您的应用程序

代码语言:javascript
运行
复制
<?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

然后

您可以像这样检索您的关注者:

代码语言:javascript
运行
复制
$tweet->get('followers/ids', array('screen_name' => 'YOUR-SCREEN-NAME-USER'));

如果要检索下一组5000名关注者,则必须添加第一次调用时的游标值。

代码语言:javascript
运行
复制
$tweet->get('followers/ids', array('screen_name' => 'YOUR-SCREEN-NAME-USER', 'cursor' => 9999999999));

您可以在以下链接中阅读:使用游标导航集合:https://dev.twitter.com/docs/misc/cursoring

票数 6
EN

Stack Overflow用户

发布于 2015-10-09 23:57:24

你不能一次卖超过200个...文档中清楚地说明了count:

每页返回的用户数,最多200。默认为20。

您可以使用以下命令通过分页来实现它

" cursor = -1“#表示第一页,”如果没有提供游标,则取值- 1,这是第一个“页”。

票数 0
EN

Stack Overflow用户

发布于 2016-03-18 07:26:32

下面是我如何在我的平台上运行/更新关注者ids的完整列表。我会避免使用sleep(),比如@aphoe script。保持这么长时间的连接真的很糟糕--如果你的用户有100万粉丝,会发生什么?你要把这个连接保持一周?如果需要,可以运行cron或保存到redis/memcache。冲洗和重复,直到你得到所有的追随者。

注意,我下面的代码是一个每分钟通过cron命令运行的类。我使用的是Laravel 5.1。所以你可以忽略很多这段代码,因为它是我的平台所独有的。例如TwitterOAuth (获取我在db上的所有oAuths ),TwitterFollowerList是另一个表,我检查条目是否已经存在,TwitterFollowersDaily是另一个表,我在其中存储/更新用户当天的总金额,TwitterApiAbraham\TwitterOAuth包。不过,您可以使用任何库。

这可能会给你一个很好的感觉,你可能会做同样的事情,甚至找出更好的方法。我不会解释所有的代码,因为发生了很多事情,但你应该能够指导它。如果你有任何问题,请告诉我。

代码语言:javascript
运行
复制
/**
 * 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;
            }
        }
    });
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17417379

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档