Joel Murphy
Joel Murphy

Reputation: 2512

Automatic follow back on Twitter

I now have a few thousand Twitter followers, and up until now I have been following them back manually. I now want to automate the process with PHP, as it can take ages to follow everyone back.

I found a twitter library for PHP created by Abraham Williams and started to write some code.

However, every time I run the script the number of users that I need to follow back is incorrect! Is this an error in my coding, or is this just how the Twitter API works?

Here's my code:

<?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();
}
?>

Upvotes: 2

Views: 4688

Answers (2)

ksg91
ksg91

Reputation: 1299

I have just ran your code on my laptop with my twitter account and I am getting correct values. Your code is correct from your side.

There is a possibility of inconsistency if number of followers or people you follow are more than 5000 or difference between them is more than 5000 because some user won't show up in last 5000 followers because after him, other 5000 users have followed you and you never followed him. So that persona will be missed.

And if your problem is that, some of the users are not getting followed while running the code, it may be because of Twitter's API rate limit.

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

check this for more: https://developer.twitter.com/en/docs/basics/rate-limiting.html So following more than 350 users will not be possible due to rate limiting by the twitter.

Upvotes: 8

Lucas
Lucas

Reputation: 10313

This works to follow/unfollow with twitteroauth.php + OAuth.php and appi v1.1 if you have more than 5000 followers/friends. The 999 limit on the follow unfollow is because of the 1000 day limit. I got started 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;
}

//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 === 999) break;
}

echo 'Unfollowed:'.$unfollow_total.'<br />';
echo 'Followed:'.$follow_total.'<br />';

Upvotes: 1

Related Questions