Get Twitter followers count using PHP

Write for Us: Familiar with Smart Home Automation, Media Streaming, HTPC, and Home Server topics? Write for us and get paid. Writing experience not required. APPLY HERE.

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.

Did this post help you?
SmartHomeBeginner brings in-depth tutorials easy enough to understand even for beginners. This takes a considerable amount of work. If this post helps you, please consider supporting us as a token of appreciation:
  • Feeling generous? Become a Sponsor (discounted options) or a Patron. You will receive privileges on our Discord Server.
  • Just want to thank us? Buy us a Coffee or a Ko-Fi.
  • May be another day? Shop on Amazon using our links. Your prices won't change but we get a small commission.
  • Don't feel like spending? You can still show your support by sharing this post, linking to it in forums, or even commenting below.

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.