Get number of Twitter followers with PHP

In: PHP

December 29th, 2010.

Get number of Twitter followers using this PHP snippet is easy. Once you get all the data from Twitter, you just have to isolate followers_count. Besides followers count, you can extract following data as well:

  1. id
  2. name
  3. screen_name
  4. location
  5. description
  6. profile_image_url
  7. url
  8. friends_count
  9. created_at
  10. lang

Now, finally, to get number of Twitter followers, use following snippet:

1
2
3
4
5
6
7
$screenName = 'stuntsnippets';
$getData = 'followers_count';
 
$xml = file_get_contents('http://twitter.com/users/show.xml?screen_name=' . $screenName);
 
if(preg_match('/' . $getData . '>(.*)</', $xml, $match) !=0)
	echo $match[1];

Notice: Don’t forget to do some PHP caching for your script. You don’t want to have too many requests toward Twitter.

Written by

Dejan is pragmatic software developer with Master's Degree in Computer Science. He is currently based in Belgrade (Serbia).