How to (Do It Yourself) Balance Followers and Friends Automatically

by xearther ~ May 4th, 2009. Filed under: Code Lounge, Internet Marketing, Twitter.

Below is the code you can run with and execute to keep your friends the same as your followers. I run this same code over at via a cron job every 10 minutes.

Be careful with this. This means if you are friends with someone and they are NOT following you… you run this code and that someone will go POOF… no mo’ friend.

Here is an example of how you would execute the code once you have it saved to a file and uploaded,

This example shows the code saved to a file named “twitterbalance.php“.

This routine makes use of the Arc90 library. You can find out more about the Arc90 library and download it via these links:

At the heart of this code is the use of the PHP array_diff() function. This is used to find out the followers that are not friends (to then “create” them as friends) and the friends that are no longer followers (to then “destroy” them as friends). The Twitter API is then used via the arc90 library to “create and destroy” friendships in order to be in balance with followers.

Here’s the code:

require_once ‘lib/Arc90/Service/Twitter.php’;
$var1 = @$_GET[‘u’] ;
$var2 = @$_GET[‘p’] ;
$username = trim($var1);
$password = trim($var2);

$friends = array();
$followers = array();

$twitter  = new Arc90_Service_Twitter($username, $password);

$errno = 0;
$errstr = ”;
$response = ”;

$xmlfollowers = simplexml_load_file(“$username.xml”);
$xmlfriends = simplexml_load_file(“$username.xml”);

foreach($xmlfollowers->children() as $child)

foreach($xmlfriends->children() as $child)



while (list($key, $val) = each($idsfolnotfri))



while (list($key, $val) = each($idsfrinotfol))

One more thing… This is the “oh, by the way” that may save you a LOT of grief. In case you think you can just drop a URL address in a CRON JOB text box and expect it to work… think again.

The reason you’re going to run into trouble? The URL has PARAMETERS.

You’ll need to do what I did: use CURL as in the following example.

/usr/bin/curl ‘’

I also set up the CRON JOB to send me an email every time it runs. I periodically open the email to see that there are no errors reported. And even though it is suggested to not use a “primary” email for the reporting, I do. I don’t mind deleting the ten million emails once a day. It does provide a realtime reassurance that the process is running. And that’s the point. I want to know if there’s a problem right away. So sending emails to a seldom used address defeats that purpose, eh?

Comments are closed.