使用abraham Twitter API库实现单向关注检测及Cursor分页获取更多数据
解决Twitter API全量获取关注/粉丝并找出单向关注用户的方案
我明白你现在的痛点——用abraham的TwitterOAuth库时,friends/list和followers/list单次只能拉200条数据,根本拿不全所有关注和粉丝对吧?别担心,Twitter API的Cursor分页就是专门解决这个问题的,我给你写个完整的可落地实现:
核心逻辑梳理
Twitter的这两个列表接口支持cursor参数来实现分页:
- 第一次请求传
cursor=-1,表示从数据集开头开始拉取 - 每次请求返回结果里会带
next_cursor,把这个值作为下一次请求的cursor参数 - 当
next_cursor=0时,就说明所有数据都已经获取完毕了
我们可以封装一个通用函数来处理这个循环分页逻辑,避免重复写代码。
完整代码实现
<?php require_once('twitteroauth/OAuth.php'); require_once('twitteroauth/twitteroauth.php'); // 替换成你的API密钥 define('CONSUMER_KEY', '你的Consumer Key'); define('CONSUMER_SECRET', '你的Consumer Secret'); define('USER_TOKEN', '你的Access Token'); define('USER_TOKEN_SECRET', '你的Access Token Secret'); // 初始化TwitterOAuth客户端 $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, USER_TOKEN, USER_TOKEN_SECRET); /** * 通用函数:获取全量的关注/粉丝列表ID * @param object $connection TwitterOAuth实例 * @param string $endpoint 接口名,比如'friends/list'或'followers/list' * @return array 所有用户的ID字符串数组 */ function get_full_twitter_list($connection, $endpoint) { $all_user_ids = []; $cursor = -1; do { // 请求数据,每次拉满200条(API单次最大限制) $response = $connection->get($endpoint, [ 'cursor' => $cursor, 'count' => 200, 'skip_status' => true, // 跳过用户状态,只拿基础信息,加快请求速度 'include_user_entities' => false // 关闭额外实体字段,减少返回数据量 ]); // 检查请求是否成功 if ($connection->getLastHttpCode() !== 200) { throw new Exception("API请求失败,HTTP状态码:{$connection->getLastHttpCode()}"); } // 提取用户ID到数组(用id_str避免大整数精度丢失) foreach ($response->users as $user) { $all_user_ids[] = $user->id_str; } // 更新cursor为下一页标识 $cursor = $response->next_cursor_str; // 加延迟避免触发API速率限制 sleep(1); } while ($cursor != 0); // cursor为0时停止循环 return $all_user_ids; } try { // 获取所有关注的用户ID echo "正在获取你的关注列表...\n"; $following_ids = get_full_twitter_list($connection, 'friends/list'); // 获取所有粉丝的用户ID echo "正在获取你的粉丝列表...\n"; $follower_ids = get_full_twitter_list($connection, 'followers/list'); // 找出:你关注了,但对方没关注你的用户ID $unfollowed_me_ids = array_diff($following_ids, $follower_ids); echo "\n你关注但未回关的用户数量:" . count($unfollowed_me_ids) . "\n"; echo "用户ID列表:\n"; print_r($unfollowed_me_ids); // (可选)批量获取这些用户的详细信息 if (!empty($unfollowed_me_ids)) { // users/lookup接口单次最多拿100个ID,所以分批处理 $id_batches = array_chunk($unfollowed_me_ids, 100); $unfollowed_users = []; foreach ($id_batches as $batch) { $users = $connection->get('users/lookup', ['user_id' => implode(',', $batch)]); if ($connection->getLastHttpCode() === 200) { $unfollowed_users = array_merge($unfollowed_users, $users); } sleep(1); } echo "\n详细用户信息:\n"; foreach ($unfollowed_users as $user) { echo "@{$user->screen_name} (ID: {$user->id_str}) - {$user->name}\n"; } } } catch (Exception $e) { echo "程序出错:" . $e->getMessage() . "\n"; } ?>
重要细节提醒
- 用
id_str而非id:Twitter用户ID是超大整数,PHP处理时容易出现精度丢失,id_str是字符串形式,完全避免这个问题 - 速率限制:Twitter对
friends/list和followers/list的限制是15分钟内15次请求,加sleep(1)完全能避开限流 - 错误处理:加入了HTTP状态码检查,方便你快速排查请求失败的原因
- 效率优化:关闭了
skip_status和include_user_entities,减少不必要的数据传输,加快请求速度
直接替换你的API密钥就能运行,完美解决全量数据获取和单向关注用户筛选的需求!
内容的提问来源于stack exchange,提问作者sercan
相关产品推荐
相关产品推荐

