Get Twitter followers count using PHP

Recently, I wrote a post on how to get Facebook like count using PHP. Continuing along the same lines, in this post, I will explain how to get Twitter followers count using a simple PHP script. The number can be stored in cache or a database and displayed as simple text or combined with other numbers to display a total followers/subscribers count. It also slightly improves page load speeds. For this reason you may find that many of the blogs just provide a link to their Twitter page. I personally like to provide a like button on the blog, which makes it convenient for the visitors to follow with just a couple of clicks. So without further delay, here it goes.

Get Twitter Followers Count
Twitter Follow Button

PHP code to get Twitter Followers Count

The PHP script to get Twitter followers count is pretty simple and straightforward:

https://www.smarthomebeginner.com/wp-admin/post.php?post=3196&action=edit&message=1#suggestion-4
$twurl = "http://twitter.com/users/show/htpcBeginner";
$response = file_get_contents ( $twurl );
$t_profile = new SimpleXMLElement ( $response );
$tcount = $t_profile->followers_count;

$twurl is the variable to hold your Twitter page. Replace htpcBeginner with your account name. You can get it from your Twitter page:

Twitter Username
Twitter Username

After you get Twitter followers count, you can display it as simple text using the following PHP code:

echo $tcount.' follow htpcbeginner.com';

This will output 7 follow htpcbeginner.com.

You can even get counts of your Facebook likes and Feedburner subscribers and add them all together and display a combined followers count like this:

Combined Followers Subscribers Count
Combined Followers Subscribers Count

Twitter Followers Count History

Although you may be able to get Twitter followers count history and more by other means, I like to record the data in a format I like. I currently get Facebook like count, Twitter followers count, FeedBurner subscribers count, and Alexa site rank and record them in a MySQL database daily to see the growth of my blog. If you make any changes / improvements to your blog then you have the data to see what impact it had on your followers count. In a separate post, I will explain how to accomplish this.

Meanwhile, get Twitter followers count using the above script, get creative, and find innovative ways to use the count. Please do not forget to share your idea with us in the comments sections.

Be the 1 in 200,000. Help us sustain what we do.
31 / 150 by Dec 31, 2024
Join Us (starting from just $1.67/month)

Anand

Anand is a self-learned computer enthusiast, hopeless tinkerer (if it ain't broke, fix it), a part-time blogger, and a Scientist during the day. He has been blogging since 2010 on Linux, Ubuntu, Home/Media/File Servers, Smart Home Automation, and related HOW-TOs.