Peter Hinchley

Who Is and Isn't Following You on Twitter?

Tagged: php, twitter

It isn't possible to easily find out all the people on Twitter that you follow who don't follow you back, or conversely, all the people who follow you that you don't follow back. To solve this little dilemma, I've written a small PHP script that leverages the Twitter API.

The script makes two API calls. The first retrieves a list of your friends (the people you follow), and the second retrieves a list of your followers. The PHP function array_diff is then used to determine the differences between the lists.

As the Twitter API limits the result set for each request to 100 users, it is necessary to use a cursor to submit additional requests (it works like pagination) until the entire data set is retrieved. Unfortunately the next_cursor value returned in each query is a number that exceeds the maximum integer supported by PHP on a 32-bit system, which in turn affects the result produced by the json_decode function. This issue is addressed by converting next_cursor to a string prior to calling json_decode.

You must have PHP installed on your computer to use the script. Save the code into a file named C:\Temp\TwitterUsers.php, set the value of $name to the screen name of the user you want to query (it can be you, or another user), open a command prompt, navigate to C:\Temp, and execute:

php TwitterUsers.php

The script will output a list of users that follow you, which you do not follow back, and then a second list of users that you follow, which do not follow you back.

Note: The Twitter API limits the number of requests that can be made from a single IP address to 150 per hour. So go easy.

<?php
$name = "hinchley";

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

$pattern = "/\"next_cursor\":([0-9]+),/";
$replace = "\"next_cursor\":\"$1\",";

$cursor = "-1";
while ($cursor != "0") {
  $handle = fopen("http://twitter.com/statuses/friends/$name.json?cursor=$cursor", "r");

  $json = "";
  while (!feof($handle)) {
    $json .= fread($handle, 8192);
  }
  fclose($handle);

  $jsonstr = preg_replace($pattern, $replace, $json);

  $data = json_decode($jsonstr);
  foreach ($data->users as $user) {
    $friends[] = $user->screen_name;
  }
  $cursor = $data->next_cursor;
}

$cursor = "-1";
while ($cursor != "0") {
  $handle = fopen("http://twitter.com/statuses/followers/$name.json?cursor=$cursor", "r");

  $json = "";
  while (!feof($handle)) {
    $json .= fread($handle, 8192);
   }
  fclose($handle);

  $jsonstr = preg_replace($pattern, $replace, $json);

  $data = json_decode($jsonstr);
  foreach ($data->users as $user) {
    $followers[] = $user->screen_name;
  }
  $cursor = $data->next_cursor;
}

$notfriends = array_diff($followers, $friends);
$notfollowers = array_diff($friends, $followers);

echo "$name is not friends with:\n";
foreach ($notfriends as $user) {
  echo "$user\n";
}

echo "\n$name is not followed by:\n";
foreach ($notfollowers as $user) {
  echo "$user\n";
}
?>

Your Say