gpt4 book ai didi

php - 在 Twitter 上自动关注

转载 作者:行者123 更新时间:2023-12-02 06:39:25 25 4
gpt4 key购买 nike

我现在有几千个 Twitter 关注者,到目前为止,我一直在手动关注他们。我现在想用 PHP 自动化这个过程,因为它可能需要很长时间才能让每个人都回来。

我找到了一个由 Abraham Williams 创建的 PHP 推特库,并开始编写一些代码。

但是,每次我运行脚本时,我需要关注的用户数量都不正确!这是我的编码错误,还是这就是 Twitter API 的工作方式?

这是我的代码:

<?php

require_once 'twitteroauth/twitteroauth.php';

define('CONSUMER_KEY', '');
define('CONSUMER_SECRET', '');
define('ACCESS_TOKEN', '');
define('ACCESS_TOKEN_SECRET', '');

ob_start();
set_time_limit(0);

function autoFollow($action){
//auth with twitter.
$toa = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);

//get the last 5000 followers
$followers = $toa->get('followers/ids', array('cursor' => -1));
$followerIds = array();

foreach ($followers->ids as $i => $id) {
$followerIds[] = $id;
}

//get the last 5000 people you've followed
$friends = $toa->get('friends/ids', array('cursor' => -1));
$friendIds = array();
foreach ($friends->ids as $i => $id) {
$friendIds[] = $id;
}


if($action=="unfollow"){
//unfollow all users that aren't following back.
$usersNotFollowingBackcount = 0;
$usersNotFollowingBack = array();

foreach($friendIds as $id){
if(!in_array($id,$followerIds) ){
array_push($usersNotFollowingBack, $id);
//unfollow the user
//$toa->post('friendships/destroy', array('id' => $id));
$usersNotFollowingBackcount++;
echo $usersNotFollowingBackcount.' users unfollowed so far</br>';
ob_flush();
flush();
}
}

echo sizeof($usersNotFollowingBack).' users who weren\'t following you back have now been unfollowed!';
}
if($action =="follow"){
//follow all users that you're not following back.
$usersYoureNotFollowingBackcount = 0;
$usersYoureNotFollowingBack = array();

foreach($followerIds as $id){
if(!in_array($id,$friendIds) ){
array_push($usersYoureNotFollowingBack, $id);
//follow the user
//$toa->post('friendships/create', array('id' => $id));
$usersYoureNotFollowingBackcount++;
echo $usersYoureNotFollowingBackcount.' users followed back so far</br>';
ob_flush();
flush();
}
}

echo sizeof($usersYoureNotFollowingBack).' users have been followed back!';
}
}

if($_GET['action']){
autoFollow($_GET['action']);
ob_end_flush();
}
?>

最佳答案

我刚刚使用我的 Twitter 帐户在我的笔记本电脑上运行了您的代码,我得到了正确的值。从您的角度来看,您的代码是正确的。

如果关注者或您关注的人的数量超过 5000 或他们之间的差异超过 5000,则可能会出现不一致,因为某些用户不会出现在最后 5000 名关注者中,因为在他之后,其他 5000 名用户已经关注你和你从来没有跟着他。所以那个角色会被错过。

如果您的问题是,某些用户在运行代码时没有得到关注,可能是因为 Twitter 的 API 速率限制。

OAuth calls are permitted 350 requests per hour and are measured against the oauth_token used in the request.

查看更多信息:https://developer.twitter.com/en/docs/basics/rate-limiting.html因此,由于 Twitter 的速率限制,将无法关注超过 350 位用户。

关于php - 在 Twitter 上自动关注,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11126560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com