PHP TWITTER bot to follow/unfollow using api version 1.1 and cursors

前端 未结 2 993
粉色の甜心
粉色の甜心 2021-02-04 19:56

This code should only unfollow users who are not following back, but it unfollows SOME followers, cannot figure out why.

$oTwitter = new TwitterOAuth (...)

$aFo         


        
相关标签:
2条回答
  • 2021-02-04 20:24

    This works, I think maybe it can help novices like me. Andy Jones answer really helped. Used great library for many other things as well here.

      require_once 'twitteroauth.php';
    
     $oTwitter = new TwitterOAuth 
    (   'YOUR_TWITTER_APP_CONSUMER_KEY',
    'YOUR_TWITTER_APP_CONSUMER_SECRET',
    'YOUR_TWITTER_APP_OAUTH_TOKEN',
    'YOUR_TWITTER_APP_OAUTH_SECRET');
     
    

    //FULL FOLLOWERS ARRAY WITH CURSOR ( FOLLOWERS > 5000)

    $e = 1;
    $cursor = -1;
    $full_followers = array();
    do {
    
    $follows = $oTwitter->get("followers/ids.json?screen_name=yourusername&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 following:" .$e. "<br /><br />";
    

    //FULL FRIEND ARRAY WITH CURSOR (FOLLOWING > 5000)

    $e = 1;
    $cursor = -1;
    $full_friends = array();
    do {
    
      $follow = $oTwitter->get("friends/ids.json?screen_name=yourusername&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);
    

    //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 "$iFollow: ".( $isFollowing ? 'OK' : '!!!' )."<br/>";
        $index++;
         if( !$isFollowing )
            {
            $parameters = array( 'user_id' => $iFollow );
            $status = $oTwitter->post('friendships/destroy', $parameters);
            $unfollow_total++;
            } if ($unfollow_total === 5) break;
        }
    

    //IF USER IS FOLLOWING ME AND I AM NOT, I FOLLOW

    $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 === 5) break;
    }
    
    echo 'Unfollowed:'.$unfollow_total.'<br />';
    echo 'Followed:'.$follow_total.'<br />';
    
    0 讨论(0)
  • 2021-02-04 20:39

    If your of followers is greater than 5000 then $aFollowers = $oTwitter->get('followers/ids'); will only return the first 5000 ids. In what order? Twitter does not guarantee any order, so we'll just assume random.

    If the following check $isFollowing = in_array( $iFollowing, $aFollowers );, the person $iFollowing may or may not be in the list $aFollowers depending on how Twitter returned the followers to you. If the person is in the first 5000, then this will work, if they're outside the first 5000 then the check will fail, even if the person is legitimately following you.

    You'll need to pull all your followers via cursors. Check out the doc on cursors / pages - will help you out a bit. Basically you need to do this.

    $aFollowers = array();
    $cursor = -1;
    do {
      $follows = $oTwitter->get('followers/ids?cursor=' . $cursor);
      $aFollowers = array_merge($follows->ids, $aFollowers);
      $cursor = $follows->next_cursor;
    } while ($cursor > 0);
    
    0 讨论(0)
提交回复
热议问题