Get Twitter Follower Count with PHP

By Avinash

January 02, 2012Tips & Tricks18 Comments

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.

$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
  $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


Share This Article

  • Aleks

    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…

    • http://www.xpertdeveloper.com Avinash

      Aleks, Glad to help, Do not forget to follow us not twitter @ XpertDevelopers also Like us on facebook.

  • http://ranindrastia.wordpress.com Rani

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

    • http://www.xpertdeveloper.com Avinash

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

  • Enrique

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

    • http://www.xpertdeveloper.com Avinash

      Glad to Help, Don’t forget to Follow us on Twitter, like us on FB

  • http://www.ariona.net Rian Ariona

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

  • http://www.elzocoloco.es Chris

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

  • Amit Kumar Upadhyay

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

    please help me……..

  • Alexandre Parra da Silva

    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!

    • http://www.xpertdeveloper.com Avinash

      Hello Alexandre,

      I will get that checked and let you know….

      • Alexandre Parra da Silva

        Thank you Avinash!

        • http://www.xpertdeveloper.com Avinash

          Yes it should work with that too….

          • Alexandre Parra da Silva

            Thanks for your reply!

  • http://www.icetheme.com Alvin Konda

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

  • MM

    Like it…….

  • ana

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

    • http://www.xpertdeveloper.com/ Avinash

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