How to get Twitter follower count in PHP

Are you are WordPress developer and want to show twitter follower count on your wordpress blog or you want to show your Twitter follower count on your PHP based site? You might be wondering how to get that number from twitter. So here is the code snippet with details so you can write your own or you can just copy paste this one.

The new Twitter API allows you to read user information in XML and JSON format, so all you have to do is get the information in XML format and read the value between <followers_count> element.

Here is the code snippet for reading twitter followers count in PHP.

$username = "hameedullah";    // Set this to username whose follower count you want to read.
$twitter_api_url = "$username";
$api = curl_init($twitter_api_url);
curl_setopt($api, CURLOPT_HEADER, 0);
curl_setopt($api, CURLOPT_RETURNTRANSFER, 1);
$xml = curl_exec($api);
if (preg_match('/followers_count>(.*)</',$xml,$match)!=0) {
    $followers_count = $match[1];
echo $followers_count;

If you have any issues with the code snippet let me know in comments, or if you are looking for a code to get something else from the Twitter API let me know in comments too.

Published by

Hameedullah Khan

VR enthusiast, Cloud Architect, Passionate Programmer and Opensource advocate.

One thought on “How to get Twitter follower count in PHP”

  1. Really nice snippet… do you know how to get e.g. 5 different twitter-account at the same time? I’m not quite familiar with php and “for” and the “while”-stuffs 😉 For example, I’d like to know the follower counts of the twitter-names “example1”, “example2”, “example3”, “example4”, “example5”. And I’d like only to start this php-file once and wanna get theses informations as a text-result.


Leave a Reply to Rockbä | Sebastian Pertsch Cancel reply

Your email address will not be published. Required fields are marked *

CommentLuv badge