Full-service Internet Marketing & Web Development
Recent Posts

Sponsors
![]() |
How to: Use the Twitter API to remove all friends who don't follow youDawn Rossi, 05-26-2009 |
The Twitter API (RESTful) is very easy to use, requiring a single curl call to perform all functions.
For example, fetching a list of all friends can be done with a single curl call:
curl -u user:password http://twitter.com/statuses/friends.xml
As part of this post, I'll demonstrate a practical example of using the Twitter API from PHP.
In this example, we'll use the Twitter API to remove all friends (people we follow) who are not following us.
// Set these for easier access
$username = "USERNAME";
$password = "PASSWORD";
// Dont change anything below this line
$page = 1;
do
{
// Fetch list of all friends
// And store them in an array
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_URL,"http://twitter.com/statuses/friends.xml?page=$page");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);
curl_close($ch);
// Advance to next page
$page++;
// Convert returned xml into array
$arr_xml = xml2ary($result);
if (empty($arr_xml['users']['_c']['user'])) break;
// Store all friend id's
foreach ($arr_xml['users']['_c']['user'] as $record)
{
$arr_friends[] = $record['_c']['screen_name']['_v'];
}
} while (1);
// DEBUG
echo "* You have a total of ".count($arr_friends)." friends.\r\n";
$page = 1;
do
{
// Fetch list of all followers
// And store them in an array
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_URL,"http://twitter.com/statuses/followers.xml?page=$page");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);
curl_close($ch);
// Advance to next page
$page++;
// Convert returned xml into array
$arr_xml = xml2ary($result);
if (empty($arr_xml['users']['_c']['user'])) break;
// Store all friend id's
foreach ($arr_xml['users']['_c']['user'] as $record)
{
$arr_followers[] = $record['_c']['screen_name']['_v'];
}
} while (1);
// DEBUG
echo "* You have a total of ".count($arr_followers)." followers.\r\n";
// Iterate through all friends
if (!empty($arr_friends))
foreach ($arr_friends as $friend_id)
{
// If this friend is NOT also a follower
if (!isStringInArray($friend_id, $arr_followers))
{
// DEBUG
echo "- Removing: ".$friend_id."\r\n";
@ob_flush();
@flush();
$cnt_removed++;
// Remove this user from the friends list
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_URL,"http://twitter.com/friendships/destroy.xml?screen_name=".$friend_id);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST,1);
$result = curl_exec($ch);
curl_close($ch);
}
}
// DEBUG
echo "* Removed a total of ".($cnt_removed+0)." friends who were not followers.\r\n";
// XML to Array
function xml2ary(&$string)
{
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parse_into_struct($parser, $string, $vals, $index);
xml_parser_free($parser);
$mnary=array();
$ary=&$mnary;
foreach ($vals as $r)
{
$t=$r['tag'];
if ($r['type']=='open')
{
if (isset($ary[$t]))
{
if (isset($ary[$t][0])) $ary[$t][]=array(); else $ary[$t]=array($ary[$t], array());
$cv=&$ary[$t][count($ary[$t])-1];
} else $cv=&$ary[$t];
if (isset($r['attributes']))
{
foreach ($r['attributes'] as $k=>$v) $cv['_a'][$k]=$v;
}
$cv['_c']=array();
$cv['_c']['_p']=&$ary;
$ary=&$cv['_c'];
}
elseif ($r['type']=='complete')
{
if (isset($ary[$t]))
{ // same as open
if (isset($ary[$t][0])) $ary[$t][]=array(); else $ary[$t]=array($ary[$t], array());
$cv=&$ary[$t][count($ary[$t])-1];
}
else $cv=&$ary[$t];
if (isset($r['attributes']))
{
foreach ($r['attributes'] as $k=>$v) $cv['_a'][$k]=$v;
}
$cv['_v']=(isset($r['value']) ? $r['value'] : '');
} elseif ($r['type']=='close')
{
$ary=&$ary['_p'];
}
}
_del_p($mnary);
return $mnary;
}
// _Internal: Remove recursion in result array
function _del_p(&$ary)
{
foreach ($ary as $k=>$v)
{
if ($k==='_p') unset($ary[$k]);
elseif (is_array($ary[$k])) _del_p($ary[$k]);
}
}
// Insert element into array
function ins2ary(&$ary, $element, $pos)
{
$ar1=array_slice($ary, 0, $pos); $ar1[]=$element;
$ary=array_merge($ar1, array_slice($ary, $pos));
}
// Check if a given string appears in a string array
function isStringInArray($Str, $Array, $count=-1)
{
if ($count==-1)
{
$count = count($Array);
}
else
{
$count = min($count,count($Array));
}
for ($i=0; $i<$count; $i++)
{
if (Strcasecmp($Str, $Array[$i])==0)
{
return true;
}
}
return false;
}
Note: Unless your Twitter application is white-listed, you are not allowed to hit the Twitter API more than 100 times per hour. When that happens, you will be blocked and have to retry an hour later. Keep that in mind when running this script.
For example, fetching a list of all friends can be done with a single curl call:
curl -u user:password http://twitter.com/statuses/friends.xml
As part of this post, I'll demonstrate a practical example of using the Twitter API from PHP.
In this example, we'll use the Twitter API to remove all friends (people we follow) who are not following us.
// Set these for easier access
$username = "USERNAME";
$password = "PASSWORD";
// Dont change anything below this line
$page = 1;
do
{
// Fetch list of all friends
// And store them in an array
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_URL,"http://twitter.com/statuses/friends.xml?page=$page");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);
curl_close($ch);
// Advance to next page
$page++;
// Convert returned xml into array
$arr_xml = xml2ary($result);
if (empty($arr_xml['users']['_c']['user'])) break;
// Store all friend id's
foreach ($arr_xml['users']['_c']['user'] as $record)
{
$arr_friends[] = $record['_c']['screen_name']['_v'];
}
} while (1);
// DEBUG
echo "* You have a total of ".count($arr_friends)." friends.\r\n";
$page = 1;
do
{
// Fetch list of all followers
// And store them in an array
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_URL,"http://twitter.com/statuses/followers.xml?page=$page");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);
curl_close($ch);
// Advance to next page
$page++;
// Convert returned xml into array
$arr_xml = xml2ary($result);
if (empty($arr_xml['users']['_c']['user'])) break;
// Store all friend id's
foreach ($arr_xml['users']['_c']['user'] as $record)
{
$arr_followers[] = $record['_c']['screen_name']['_v'];
}
} while (1);
// DEBUG
echo "* You have a total of ".count($arr_followers)." followers.\r\n";
// Iterate through all friends
if (!empty($arr_friends))
foreach ($arr_friends as $friend_id)
{
// If this friend is NOT also a follower
if (!isStringInArray($friend_id, $arr_followers))
{
// DEBUG
echo "- Removing: ".$friend_id."\r\n";
@ob_flush();
@flush();
$cnt_removed++;
// Remove this user from the friends list
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_URL,"http://twitter.com/friendships/destroy.xml?screen_name=".$friend_id);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST,1);
$result = curl_exec($ch);
curl_close($ch);
}
}
// DEBUG
echo "* Removed a total of ".($cnt_removed+0)." friends who were not followers.\r\n";
// XML to Array
function xml2ary(&$string)
{
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parse_into_struct($parser, $string, $vals, $index);
xml_parser_free($parser);
$mnary=array();
$ary=&$mnary;
foreach ($vals as $r)
{
$t=$r['tag'];
if ($r['type']=='open')
{
if (isset($ary[$t]))
{
if (isset($ary[$t][0])) $ary[$t][]=array(); else $ary[$t]=array($ary[$t], array());
$cv=&$ary[$t][count($ary[$t])-1];
} else $cv=&$ary[$t];
if (isset($r['attributes']))
{
foreach ($r['attributes'] as $k=>$v) $cv['_a'][$k]=$v;
}
$cv['_c']=array();
$cv['_c']['_p']=&$ary;
$ary=&$cv['_c'];
}
elseif ($r['type']=='complete')
{
if (isset($ary[$t]))
{ // same as open
if (isset($ary[$t][0])) $ary[$t][]=array(); else $ary[$t]=array($ary[$t], array());
$cv=&$ary[$t][count($ary[$t])-1];
}
else $cv=&$ary[$t];
if (isset($r['attributes']))
{
foreach ($r['attributes'] as $k=>$v) $cv['_a'][$k]=$v;
}
$cv['_v']=(isset($r['value']) ? $r['value'] : '');
} elseif ($r['type']=='close')
{
$ary=&$ary['_p'];
}
}
_del_p($mnary);
return $mnary;
}
// _Internal: Remove recursion in result array
function _del_p(&$ary)
{
foreach ($ary as $k=>$v)
{
if ($k==='_p') unset($ary[$k]);
elseif (is_array($ary[$k])) _del_p($ary[$k]);
}
}
// Insert element into array
function ins2ary(&$ary, $element, $pos)
{
$ar1=array_slice($ary, 0, $pos); $ar1[]=$element;
$ary=array_merge($ar1, array_slice($ary, $pos));
}
// Check if a given string appears in a string array
function isStringInArray($Str, $Array, $count=-1)
{
if ($count==-1)
{
$count = count($Array);
}
else
{
$count = min($count,count($Array));
}
for ($i=0; $i<$count; $i++)
{
if (Strcasecmp($Str, $Array[$i])==0)
{
return true;
}
}
return false;
}
Note: Unless your Twitter application is white-listed, you are not allowed to hit the Twitter API more than 100 times per hour. When that happens, you will be blocked and have to retry an hour later. Keep that in mind when running this script.
![]() |
Marcos, 07-01-2009 |
Dear fellows, with regards to this script, I have many many pages (22.000 followers/friends) so there is not enough memory to run the script. :-(
How possible it is to limit, let's say to a few pages? something like creating two more variables: $startpage and $endpage for example?
By doing that you could call the script to execute a few pages at a time...
Any PHP expert willing to do this? :-)
How possible it is to limit, let's say to a few pages? something like creating two more variables: $startpage and $endpage for example?
By doing that you could call the script to execute a few pages at a time...
Any PHP expert willing to do this? :-)
![]() |
dryllenz, 07-02-2009 |
What if not all like, i want to delete two followers out of five followers
![]() |
Ross, 07-14-2009 |
Marcos: Look at the variable $page, make it your start page. Instead of 1 make it $start and just stop incrementing at the $end variable in the do while statement.
dryllenz: Add one variable to catch the 5 or other number. Include it in your conditional statement. Just make a skip after two followers are deleted then jump to another batch of 5.
dryllenz: Add one variable to catch the 5 or other number. Include it in your conditional statement. Just make a skip after two followers are deleted then jump to another batch of 5.
![]() |
newbie, 12-31-2009 |
can someone help me run this? i uploaded but just shows text...
|
|
Subscribe Now to receive new posts via Email as soon as they come out.
Comments
Post your comments





