Tips & Tricks

Get Twitter Follower Count with PHP

In previous post I have explined about getting the Facebook page details using Graph API and PHP. Now this post is about to get the Twitter Follower Count using PHP.

For this method what you will need is just and User Name of the Twitter Account. For Example Expert Developer’s Twitter Account is XpertDevelopers.

How it Works

Like Facebook Graph API, you just make one call to twitter URL using you twitter account name. So our twitter account is XpertDevelopers, then that URL will be http://twitter.com/users/show/XpertDevelopers

You will need to get the content of this URL and have to parse the content to get the Follower Count. Let’s see how to get this done with example.

[cc lang=”php”]
$url = “http://twitter.com/users/show/XpertDevelopers”;
$response = file_get_contents ( $url );
$t_profile = new SimpleXMLElement ( $response );
$count = $t_profile->followers_count;

echo “Expert Developer Twitter Followers : “.$count;

// Output
Expert Developer Twitter Followers : 73
[/cc]


Shares:
  • Aleks
    Aleks
    April 30, 2012 at 4:42 am

    No cache, no fancy javascript, no overcomplicated stuff, no CSS for styling, no nothing.
    Just a simpleXMLElement.

    You sir, are my hero… NONE of these other crap tutorials around have actually worked, or it has allways had a bug in them, or just a general shitty support, or depends on a SHITLOAD of libraries. This… So simple, so clean… So just… Exactly what I was looking for…

    Reply
  • Rani
    July 12, 2012 at 3:50 pm

    awesome!! btw, beside the followers number, what else I can get with this method?

    Reply
    • Avinash
      July 14, 2012 at 12:17 pm

      As per the above code you can just print the $t_profile variable and can see the things you can get..

      Reply
  • kabeza
    kabeza
    July 14, 2012 at 12:59 am

    Just was i was looking for, no oauth, nor sdk, nor anything. Direct access and voila. Thanks !!

    Reply
  • Rian Ariona
    October 15, 2012 at 7:28 am

    Wow, it’s work.. thanks for this code snippet :)

    Reply
  • Chris
    November 29, 2012 at 4:46 am

    Finally! I was looking for this, but couldn’t find a good way to get the number. Thanks!

    Reply
  • Amit Kumar Upadhyay
    Amit Kumar Upadhyay
    December 17, 2012 at 5:31 pm

    Very good article thank you
    but i want a follwoing and tweets count as you show the Followers

    please help me……..

    Reply
  • Alexandre Parra da Silva
    Alexandre Parra da Silva
    January 17, 2013 at 10:36 pm

    Hello Avinash,

    This method is very good for its simplicity, as others have stated.
    Do you know if it will still be available after the API v1.0 deprecation scheduled for coming March?
    Thank you!

    Reply
    • Avinash
      January 18, 2013 at 9:07 am

      Hello Alexandre,

      I will get that checked and let you know….

      Reply
      • Alexandre Parra da Silva
        Alexandre Parra da Silva
        January 18, 2013 at 3:22 pm

        Thank you Avinash!

        Reply
        • Avinash
          January 20, 2013 at 4:20 pm

          Yes it should work with that too….

          Reply
          • Alexandre Parra da Silva
            Alexandre Parra da Silva
            January 22, 2013 at 5:39 pm

            Thanks for your reply!

  • Alvin Konda
    February 4, 2013 at 9:05 pm

    Thanks, Thanks, Thanks. Tried tens of times and now it works fine.

    Reply
  • MM
    MM
    February 14, 2013 at 8:13 am

    Like it…….

    Reply
  • ana
    ana
    April 30, 2013 at 4:32 pm

    great share. here is a script for tweet url counter:

    Reply
    • Avinash
      April 30, 2013 at 10:56 pm

      Hi Ana, Your link was using old version of API that’s why it is removed from comment

      Reply

Leave a Reply

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