[PHP] Displaying the amount of twitter followers you have

Results 1 to 13 of 13
  1. #1
    Account Upgraded | Title Enabled! MattehCarter is offline
    MemberRank
    Apr 2011 Join Date
    England, UKLocation
    245Posts

    [PHP] Displaying the amount of twitter followers you have

    Well, I section of my latest website had to display the amount of twitter followers that I had. After searching around and finding stupidly long and unclean functions, I decided to create my own..

    PHP Code:
                    <?php
                        
    function getTwitterFollowers($userid) {
                            
    $url "http://twitter.com/users/show.xml?screen_name=$userid";
                            
    $xml simplexml_load_file($url) or die ("Error fetching twitter XML");
                            echo 
    $xml->followers_count " followers.";
                        }
                    
    ?>
    Might come in useful for someone :) you can also modify it for other things such as displaying your latest tweet.

    Usage
    PHP Code:
    getTwitterFollowers("USERNAME"); 
    :)


  2. #2
    Member Darky is offline
    MemberRank
    Oct 2008 Join Date
    Fun landLocation
    92Posts

    Re: [PHP] Displaying the amount of twitter followers you have

    nvm.... mistake :D
    Last edited by Darky; 05-06-11 at 05:19 AM.

  3. #3
    Infraction Baɴɴed holthelper is offline
    MemberRank
    Apr 2008 Join Date
    1,765Posts

    Re: [PHP] Displaying the amount of twitter followers you have

    so this is more of a release then asking for help, gotcha

  4. #4
    Account Upgraded | Title Enabled! MattehCarter is offline
    MemberRank
    Apr 2011 Join Date
    England, UKLocation
    245Posts

    Re: [PHP] Displaying the amount of twitter followers you have

    Yeah, I posted in the wrong section but I've requested it to be moved - just waiting for the moderator to move it :)

  5. #5
    Alpha Member Justei is offline
    MemberRank
    Oct 2007 Join Date
    /f241Location
    1,904Posts

    Re: [PHP] Displaying the amount of twitter followers you have

    Im gonna move it asap when i get home, this phone ui wont let me move threads :/
    Posted via Mobile Device

  6. #6
    Goddess Madison is offline
    Super ModRank
    Jan 2007 Join Date
    11,999Posts

    Re: [PHP] Displaying the amount of twitter followers you have

    Its not a bad idea, and I'm glad to see you attempt to handle what to do if you can't connect to Twitter, but die is NOT the answer.

    Die is going to halt your whole script from running, and I personally wouldn't want to rely on Twitter that much. If you can't connect then return something else, or do something else there, but don't use a die statement.

  7. #7
    Hm. foxx is offline
    MemberRank
    Sep 2006 Join Date
    Czech RepublicLocation
    5,257Posts

    Re: [PHP] Displaying the amount of twitter followers you have

    and what I wouldn't use is simple_xml_load which I believe is slow.. or slower than this anyway

    PHP Code:
    function followersNum($id) {
      
    $jsonArray json_decode('http://api.twitter.com/1/users/show/'.$id.'.json?callback=?'true);
      return 
    $jsonArray['followers_count'];

    or what I'd do because it's more fancy
    Edit this Fiddle - jsFiddle - Online Editor for the Web (JavaScript, MooTools, jQuery, Prototype, YUI, Glow and Dojo, HTML, CSS)

  8. #8
    Account Upgraded | Title Enabled! MattehCarter is offline
    MemberRank
    Apr 2011 Join Date
    England, UKLocation
    245Posts

    Re: [PHP] Displaying the amount of twitter followers you have

    Okay, well it's my first time ever fetching data from an XML file, so it was quite basic. What should I use instead of die() ?

    @foxx the XML file is very unreliable. I'm using a similar script to load my latest tweet, whenever I retweet someone the XML always errors. And I'm going to take a look at the link now :)

  9. #9
    Web Developer Markshall is offline
    MemberRank
    Oct 2009 Join Date
    EnglandLocation
    628Posts

    Re: [PHP] Displaying the amount of twitter followers you have

    Quote Originally Posted by MattehCarter View Post
    Okay, well it's my first time ever fetching data from an XML file, so it was quite basic. What should I use instead of die() ?

    @foxx the XML file is very unreliable. I'm using a similar script to load my latest tweet, whenever I retweet someone the XML always errors. And I'm going to take a look at the link now :)
    If you're putting the follower count on the side of the screen for example, you don't want the script to echo an ugly error message such as "Can not parse XML file from Twitter", so why not just do:

    PHP Code:
    $xml = @simplexml_load_file$url ); 
    The @ will prevent an error message being displayed if the script failed to parse the XML file.

    That's what I would do anyway.

  10. #10
    :-) s-p-n is offline
    DeveloperRank
    Jun 2007 Join Date
    Next DoorLocation
    2,098Posts

    Re: [PHP] Displaying the amount of twitter followers you have

    Well you don't want the script to die because anything after that point won't exist during the execution of that script. Also Twitter is notorious for their down-time.. Dunno if they still are, but yeh I don't tweet anymore, every other 5 minutes their servers overload or what-have-you, and that stupid message pops up about how their rushing around fixing it... every five minutes.

    And I thought we've established using the json from the twitter API makes a lot more sense than ripping through the xml... (which seems more likely to change than something provided by the API)


    Just use foxx's example, and forget about XML for today.. ;)

  11. #11
    Account Upgraded | Title Enabled! MattehCarter is offline
    MemberRank
    Apr 2011 Join Date
    England, UKLocation
    245Posts

    Re: [PHP] Displaying the amount of twitter followers you have

    Is there a way that I can also display my latest tweet via foxx's way?

  12. #12
    Hm. foxx is offline
    MemberRank
    Sep 2006 Join Date
    Czech RepublicLocation
    5,257Posts

    Re: [PHP] Displaying the amount of twitter followers you have

    easily, just wait couple of hours until I'm sober and I'll show you

  13. #13
    Account Upgraded | Title Enabled! MattehCarter is offline
    MemberRank
    Apr 2011 Join Date
    England, UKLocation
    245Posts

    Re: [PHP] Displaying the amount of twitter followers you have

    Haha :') I've got it working now! Thanks anyway though man :)



Advertisement