自动回复Twitter

时间:2012-06-20 19:13:07

标签: php twitter twitter-follow

我现在有几千个Twitter关注者,直到现在我一直在关注他们。我现在想用PHP自动完成这个过程,因为跟回所有人可能需要很长时间。

我找到了一个由亚伯拉罕·威廉姆斯创建的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();
}
?>

2 个答案:

答案 0 :(得分:8)

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

如果关注者或您关注的人数超过5000或者他们之间的差异超过5000,则可能会出现不一致的情况,因为有些用户不会出现在最后5000名关注者中,因为在他之后,其他5000位用户都关注了你和你从来没有跟着他。所以这个角色会被遗漏。

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

  

OAuth调用每小时允许350个请求,并根据请求中使用的oauth_token进行衡量。

查看此内容以获取更多信息:https://developer.twitter.com/en/docs/basics/rate-limiting.html 因此,由于推特的速率限制,将无法访问超过350名用户。

答案 1 :(得分:1)

如果您有超过5000个关注者/朋友,则可以使用twitteroauth.php + OAuth.php和appi v1.1关注/取消关注。跟随取消关注的999限制是因为1000天的限制。我开始了with this

//FULL FOLLOWERS ARRAY WITH CURSOR ( FOLLOWERS > 5000)
    $e = 0;
    $cursor = -1;
    $full_followers = array();
    do {
        //SET UP THE URL
      $follows = $oTwitter->get("followers/ids.json?screen_name=youruser&cursor=".$cursor);
      $foll_array = (array)$follows;

      foreach ($foll_array['ids'] as $key => $val) {

            $full_followers[$e] = $val;
            $e++; 
      }
           $cursor = $follows->next_cursor;

      } while ($cursor > 0);
echo "Number of followers:" .$e. "<br /><br />";

//FULL FRIEND ARRAY WITH CURSOR (FOLLOWING > 5000)
    $e = 0;
    $cursor = -1;
    $full_friends = array();
    do {

      $follow = $oTwitter->get("friends/ids.json?screen_name=youruser&cursor=".$cursor);
      $foll_array = (array)$follow;

      foreach ($foll_array['ids'] as $key => $val) {

            $full_friends[$e] = $val;
            $e++;
      }
          $cursor = $follow->next_cursor;

    } while ($cursor > 0);
    echo "Number of following:" .$e. "<br /><br />";

//IF I AM FOLLOWING USER AND HE IS NOT FOLLOWING ME BACK, I UNFOLLOW HIM
$index=1;
$unfollow_total = 0;

foreach( $full_friends as $iFollow )
{
$isFollowing = in_array( $iFollow, $full_followers );

echo $index .":"."$iFollow: ".( $isFollowing ? 'OK' : '!!!' )."<br/>";
$index++;

if( !$isFollowing )
    {
    $parameters = array( 'user_id' => $iFollow );
    $status = $oTwitter->post('friendships/destroy', $parameters);
    $unfollow_total++;
    } if ($unfollow_total++; === 999) break;
}
  

//如果用户跟随我而我不是,我跟着

$index=1;
$follow_total = 0;
foreach( $full_followers as $heFollows )
{
$amFollowing = in_array( $heFollows, $full_friends );

echo "$heFollows: ".( $amFollowing ? 'OK' : '!!!' )."<br/>";


$index++;
     if( !$amFollowing )
    {
    $parameters = array( 'user_id' => $heFollows );
    $status = $oTwitter->post('friendships/create', $parameters);
    $follow_total++;
    } if ($follow_total === 999) break;
}

echo 'Unfollowed:'.$unfollow_total.'<br />';
echo 'Followed:'.$follow_total.'<br />';
相关问题