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.

<?php
 
$username = "hameedullah";    // Set this to username whose follower count you want to read.
 
$twitter_api_url = "http://api.twitter.com/1/users/show.xml?screen_name=$username";
 
$api = curl_init($twitter_api_url);
curl_setopt($api, CURLOPT_HEADER, 0);
curl_setopt($api, CURLOPT_RETURNTRANSFER, 1);
$xml = curl_exec($api);
curl_close($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.