Get Twitter Follower Count with PHP

in WordPress Tutorials

Almost every web design / development blog, and the majority of other blogs, like to display the number of people they have following their Twitter account. Their are a variety of ways to do this, and dozens of plugins that do it for you, but as a developer it is a really good if you know how to do it without relying on a plugin.

Peter Ivanov wrote a great PHP function that does exactly what we need. Simply paste this into your functions.php:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
 * Fetch the number of followers from twitter api
 *
 * @author Peter Ivanov 
 * @copyright    http://www.ooyes.net
 * @version    0.2
 * @link http://www.ooyes.net
 * @param string $username
 * @return string
 */
function twitter_followers_counter($username) {
 
	$cache_file = CACHEDIR . 'twitter_followers_counter_' . md5 ( $username );
 
	if (is_file ( $cache_file ) == false) {
		$cache_file_time = strtotime ( '1984-01-11 07:15' );
	} else {
		$cache_file_time = filemtime ( $cache_file );
	} 
 
	$now = strtotime ( date ( 'Y-m-d H:i:s' ) );
	$api_call = $cache_file_time;
	$difference = $now - $api_call;
	$api_time_seconds = 1800;
 
	if ($difference >= $api_time_seconds) {
		$api_page = 'http://twitter.com/users/show/' . $username;
		$xml = file_get_contents ( $api_page );
 
		$profile = new SimpleXMLElement ( $xml );
		$count = $profile->followers_count;
		if (is_file ( $cache_file ) == true) {
			unlink ( $cache_file );
		}
		touch ( $cache_file );
		file_put_contents ( $cache_file, strval ( $count ) );
		return strval ( $count );
	} else {
		$count = file_get_contents ( $cache_file );
		return strval ( $count );
	}
}

This function will contact the Twitter API and obtain the follower count for the specified username, and store the results in a cache so that the count isn’t recalculated with every page load, resulting in dozens, or hundreds, of API request calls.

To display the actual follower count, simple do this anywhere in your template files:

1
print twitter_followers_counter($twitter_name);

Remember to replace $twitter_name with your Twitter username. To display this site’s follower count, we’d do:

1
print twitter_followers_counter('wpmods');

Enjoy!

Pippin

Comments (5)

  • Comment by edgars
    edgars
    says:

    change ‘ > ‘  to ‘ > ‘
    and works perfectly
     
    thanks!

  • Comment by Woody
    Woody
    says:

    Works fine for me (if you fix the & gt encoding) – perhaps you should change this example dude :)

  • Comment by Chris Clarke
    Chris Clarke
    says:

    You can fix the parse error by replacing the & gt; with > but it needs permissions that I don’t want to give it.

    I created an OAuth one

  • Comment by Valdis
    Valdis
    says:

    Too bad it does not work !
    Parse error: syntax error, unexpected ‘;’ in /hermes/bosweb26b/b2153/………

  • Comment by AJ Clarke
    AJ Clarke
    says:

    Nice piece of code, I will definitely be sharing this on my blog at some point. Is this the PHP that WPMods currently uses?