【问题标题】:Automatic follow back on Twitter在 Twitter 上自动关注
【发布时间】:2012-06-23 00:04:24
【问题描述】:

我现在有几千个 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();
}
?>

【问题讨论】:

  • 你为什么要关注这么多人,以至于你不得不把它自动化?
  • 然后你需要一个脚本来阻止来自你关注的人的所有传入推文。
  • 我在 Twitter 上关注的用户中有 80% 是朋友,所以我不介意在我的时间线上看到他们的更新 :)
  • 只是一个想法 - 你可以使用 ifttt 食谱来做到这一点,如果他们关注你,它会自动跟随用户。这是食谱:ifttt.com/recipes/19225
  • @potench - 很抱歉直到现在才回复,过去 12 天我一直在度假。 IFTTT 似乎真的很有用,我不敢相信我以前从未遇到过它!在接下来的几天里,我会尝试一下。非常感谢。

标签: php twitter twitter-follow


【解决方案1】:

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

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

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

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

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

【讨论】:

  • 好吧,至少在一小时内跟踪超过 350 个是不可能的。显然,要关注的用户可以按照 API 允许的速率排队和提交。
  • @quickshiftin 是的,但 OP 不是来回答他实际在做什么。 :\
  • 嗨@ksg91。感谢您的回答。很抱歉我现在才回复你,但我已经离开了 12 天。 ifttt.com 似乎是解决我问题的最佳方法,但还是感谢您的回复。
【解决方案2】:

如果您的关注者/朋友超过 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 />';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-20
    • 1970-01-01
    • 1970-01-01
    • 2011-09-08
    • 2011-09-14
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多