PHP Script to Follow Someone Else’s Twitter Followers

June 6, 2010

A gopher, for no reason really

I was going to write an introduction about how I don’t think that Twitter follower numbers are particularly important, but how/why I wrote this script anyway. But, to be honest, if you have a practical ethical use for this, that’s all I care about – who cares why I wrote it in the first place?

This PHP script is designed to be run on the command line. Once configured with your username, password, and the username of another twitter user, it will loop through their followers, and your account will follow each one (but remember, there are all kinds of limits in place, so you’ll probably follow a maximum of 1000 daily – and, depending on your following/follower ratio – up to a maximum of 2000 followers for your account).

One final note: I don’t use this (or anything similar) for my personal @zambonini Twitter account, in case you were wondering. This was written with a very specific purpose in mind, not as a general tool I use for my Twitter accounts.

<?php
/*
 *  NOTES
 *
 *  1. From danzambonini.com
 *  2. Please use responsibly. Don't use to spam.
 *  3. Remember there's currently a 1000 daily limit to how many you can follow.
 *  4. Plus, you might reach the 2000/whatever-ratio limit for your account.
 *  5. This needs the CURL library.
 *  6. Uses some code from http://twitter.slawcup.com/twitter.class.phps
 */

// *** SET THESE ***
$my_username                    = 'yourtwitterusername';
$my_password                    = 'yourtwitterpassword';
$user_whose_followers_to_follow = 'anothertwitterusername'; 

// Get the followers of the chosen user
$aUserIds = get_follower_ids($user_whose_followers_to_follow);

// Feedback - how many we found
$num_to_follow = count($aUserIds);

echo "{$num_to_follow} unique/new users found from {$user_whose_followers_to_follow}\n\n";

// Follow each one
foreach ($aUserIds as $id_to_follow)
{
    echo "Following {$id_to_follow}\n";
    followUser($id_to_follow, $my_username, $my_password);
}

echo "Done!\n\n";

// *** Functions ***
function get_follower_ids($username)
{
    $url = 'http://api.twitter.com/1/followers/ids.xml?screen_name=' . $username;
    $xml = file_get_contents($url);

    preg_match_all('/<id>([^<]+)<\/id>/', $xml, $aMatch);

    return $aMatch[1];
}

// Based on a function from http://twitter.slawcup.com/twitter.class.phps

function followUser($id, $my_username, $my_password)
{
    $request_url = 'http://twitter.com/friendships/create/' . (int) $id . '.xml';

    $ch = curl_init($request_url);

    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, array());
    curl_setopt($ch, CURLOPT_USERPWD, $my_username . ':' . $my_password );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_NOBODY, 0);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Twitter Follow Others Followers PHP Script');
    @curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:', 'X-Twitter-Client: ',
                                                          'X-Twitter-Client-Version: ',
                                                          'X-Twitter-Client-URL: '));

    $response = curl_exec($ch);

    curl_close($ch);
}
?>

Tags: , , , ,

3 Responses to “ PHP Script to Follow Someone Else’s Twitter Followers ”

  1. chris on June 21, 2010 at 6:55 pm

    tried this – unless twitter followers do not update instantaneously, this isn’t working for me. returns something like – followers 4592372

  2. […] when I decided to start auto-following the followers of other relevant Twitter accounts (I think I chose infoviz type accounts). At the […]

  3. Jimmy Fuentes on September 21, 2010 at 8:47 pm

    will there be any updates to this script? since the oauth is mandatory now, i dont think the script works.

Leave a Reply