May 4th, 2009

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

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 www.nichetrivia.com 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“.

http://www.YOURSITE.com/twitterbalance.php?u=YOURTWITTERNAME&p=YOURTWITTERPASSWORD

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

http://lab.arc90.com/2008/06/php_twitter_api_client.php

http://blog.arc90.com/2008/06/twitter_api_client_released.php

http://code.google.com/p/arc90-service-twitter/

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:

<?php
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(“http://www.twitter.com/followers/ids/$username.xml”);
$xmlfriends = simplexml_load_file(“http://www.twitter.com/friends/ids/$username.xml”);

$idsfollowers=array();
$x=0;
foreach($xmlfollowers->children() as $child)
{
$idsfollowers[$x]=$child;
$x+=1;
}

$idsfriends=array();
$x=0;
foreach($xmlfriends->children() as $child)
{
$idsfriends[$x]=$child;
$x+=1;
}

$idsfolnotfri=array();
$idsfolnotfri=array_diff($idsfollowers,$idsfriends);

reset($idsfolnotfri);

while (list($key, $val) = each($idsfolnotfri))
{
$twitter->createFriendship($val);
}

$idsfrinotfol=array();
$idsfrinotfol=array_diff($idsfriends,$idsfollowers);

reset($idsfrinotfol);

while (list($key, $val) = each($idsfrinotfol))
{
$twitter->destroyFriendship($val);
}
?>

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 ‘http://www.nichetrivia.com/MYBALANCINGACT.php?u=nichetrivia&p=WOULDNTYOULIKETOKNOW’

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?

by xearther