我现在有几千个 Twitter 关注者,直到现在我一直在手动关注他们。我现在想用 PHP 自动化这个过程,因为跟踪每个人可能需要很长时间。
我找到了一个由 Abraham Williams 创建的 PHP twitter 库,并开始编写一些代码。
但是,每次我运行脚本时,我需要跟进的用户数量都是不正确的!这是我的编码错误,还是 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();
}
?>