April 17th, 2009

Counting Twitter Followers and Friends

Expanding on the prior post, below is the simple code to display the number of Followers and Friends of any Twitter User. Just pass the User Name to the php file like so:

http://www.nichetrivia.com/followersfriends.php?u=xearther

The browser will then display something like:

23 Followers
8 Friends

You are welcome to use the above URL. Just replace xearther with any Twitter User Name of your choosing. And like the prior post, this code does not require any password.

Here is the code behind followersfriends.php:

<?php
$username = trim(@$_GET[‘u’]);
$xmlfollowers = simplexml_load_file(“http://www.twitter.com/followers/ids/$username.xml“);
$xmlfriends = simplexml_load_file(“http://www.twitter.com/friends/ids/$username.xml“);

$x=0;
$idsfollowers=array();
foreach($xmlfollowers->children() as $child)
{
$idsfollowers[$x]=$child;
$x+=1;
}
echo “$x Followers<br>”;

$x=0;
$idsfriends=array();
foreach($xmlfriends->children() as $child)
{
$idsfriends[$x]=$child;
$x+=1;
}
echo “$x Friends<br>”;
?>

I hope you find this interesting and somewhat useful.

On Sunday I will post the code that builds on the code above and synchronizes Friends to be exactly the same as Followers.

by xearther