Home » Home Server » Get Twitter followers count using PHP
Home Server

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.htpcbeginner.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.

About the author

Anand

Anand is a self-learned computer enthusiast, a part-time blogger, and a Food Scientist by career. He has been blogging since 2010 on Linux, Ubuntu, Home/Media/File Servers, XBMC, and related HOW-TOs (read more).

Archives

Join the other 110,000 followers