How to show the avatars of your twitter followers on your website

A simple PHP script to display twitter followers

I have seen quite a lot of people asking how to get the avatars of their Twitter followers displaying on their website. Well, as a break to custom coding an e-commerce site for a client, I have cooked up a little code that does just that.

In addition to the obvious, that it displays the avatars of your followers on your website, it has a few little extras.

  • It displays the total number of Twitter followers above the list of avatars.
  • If Twitter falls of it’s perch or decides to return an error, it doesn’t display anything, so no breakages there.
  • You need only enter your Twitter username and password in order for it to do it’s thing.

The Twitters Followers avatar list is easy to style using the image class ‘twitter_followers’.

img.twitter_followers {
padding:2px;
display:inline;
height:30px;
width:30px;
}

Would give you something like this, look at the bottom of the page. The page is a work in progress, but you get the idea. If you don’t see anything, Twitter has fallen of it’s perch or decided to pull a ‘too many tweets’ error.

If you find this ‘Twitter_followers’ script useful or have any observations, leave a comment.

[download#2#image]

32 thoughts on “How to show the avatars of your twitter followers on your website

  1. Tim Millwood

    [quote comment=”471″][quote comment=”470″]let’s see if we can integrate it into drupal![/quote]

    I know that’s not going to take you long Tim! Let me know when your done, I can picture it on http://www.millwoodonline.co.uk, very nice.[/quote]

    Well it works! I have just put it in a Drupal block on my site for now, but hope to make it into a module over the weekend with a settings page to add twitter login.

    Might also convert to show people you are following as well as people following you.

    Reply
  2. deep.thought Post author

    Looks good, I think that you might be on to something there Tim. Also might be an idea to look at caching the results, so that it only calls once in a while.

    Let’s see what kind of interest it gets, if there are lots of comments from people looking for additional features it might be fun to develop it.

    Reply
  3. Tim Millwood

    I think the biggest problem is twitter falling over.

    I was going to make each avatar a link to the users twitter page, but the block was not displaying, guess twitter API is dead for the day.

    Reply
  4. Tim Millwood

    “http://twitter.com/statuses/followers/”.$username.”.xml”
    to
    “http://twitter.com/statuses/friends/”.$username.”.xml”
    to get a list of your friends rather than your followers.

    and change the echo line to echo "screen_name, "\">profile_image_url, "\" title=\"", $twit->name, " from ", $twit->location, "\" alt=\"", $twit->name, " from ", $twit->location, "\" />\n"; to make the avatar a link and feature the friends location, because not all people have a local I could do with making this an if.

    I will have to blog about all this later, i’m having great fun!

    Reply
  5. deep.thought Post author

    I go to get a sandwich and miss all the fun lol.

    For the link you could use something like –
    a href=''www.twitter.com\'', $twit->screen_name ,'' />.....

    Which would link back to their profile. You could try a !empty($twit->url) but I am not sure if that would work, also !isset($twit->url) would be worth exploring.

    As for the caching aspect, we would have to empty a MySQL table each time we did a call and write each entry back to the database. Just concerns me that I am uncertain how to check prior to that, that we actually have new data e.g. twitter hasn’t fallen over again (no sense emptying a table if we have no data to stream to create a new one).

    I think you might get quite a good uptake of your drupal module if you could cover all the bases against twitter falling of it’s perch.

    Reply
  6. CannonGod

    I have to admit my n00biness here. I’ve searched all over the web for something like this, but sadly my skills as a web programmer are limited :¬(

    I’m trying to implement your script into my tumblr blog (http://cannongod.tumblr.com) ideally at the bottom somewhere, but I need a little guidance as I keep screwing up! >_

    Reply
  7. tj

    Anyone interested in helping me figure out a way to pull in select twitter friends (like 10) into 1 feed, sorted by publish date, with their avatar and status? Im desperate

    Reply
  8. deep.thought Post author

    [quote comment=”493″]Does this only work with php5? love the way it looks, integrating it with the twitter section of my website.[/quote]
    It’s looking good Tim, love your Facebook type theme by the way!

    Reply
  9. Marc

    Awesome script! Having a ton of fun with this at the moment, for some reason when I implement it only shows 100 followers max – cant really figure out how to show more – thoughts?

    Reply
  10. Marko

    I got an error:

    Fatal error: Cannot instantiate non-existent class: simplexmlelement in mysite.com/followers.php on line 22

    Please help!

    Reply
  11. deep.thought Post author

    [quote comment=”507″]I got an error:

    Fatal error: Cannot instantiate non-existent class: simplexmlelement in mysite.com/followers.php on line 22

    Please help![/quote]

    You need to have php5 working on your web server in order to call the simplexmlelement class. It is ‘non-existant’ in php4.

    Hope that helps.

    Reply
  12. deep.thought Post author

    [quote comment=”508″]Does anyone have the finished script with links back to the follower’s page?[/quote]

    See Tim Millwoods final comment above, that will do the trick.

    Reply
  13. decomo

    Thanks for the script. Has anyone been able to make this work on a Joomla site? Also, does the script takes any 100 avatars ( assume that’s the limit) from a random starting point or does it always show the latest 100 (or the first 100)? Thanks.

    Reply
  14. Designs Genius

    The script works fine. Unfortunately, to display a twitter avatar mosaic without twitter account link is a real problem for many people and it decrease the popularity of this script.
    I hope this beautiful project will continu to be developed.

    Kind regards – DG

    Reply
  15. Rob

    @Tim – “I think the biggest problem is twitter falling over.”

    Would a good solution be to grab the xml file and store it on the local server instead of reading the xml file from twitter.com? This way you will always a good copy of the xml.

    If the the file returns a null value or an error, it should loop around and try to grab the xml file again.

    You could have the file get updated every 15secs every day.

    I might approach it that way. That way my scripts can read the xml files locally and not be dependednt on twitter.

    Reply
  16. Pingback: Display your twitter followers on your website or blog | PunitShet.com

  17. John

    This script seems to be broken now. I had it working and it was great, but within the last week or so, it has stopped working returning a ‘String could not be parsed as XML’ error. Did something change with the API?

    Reply
  18. mic

    hi, i try use you code but i cant connect with tweeter maybe change this.

    the code:
    $target = “http://twitter.com/statuses/followers/”.$username.”.xml”;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $target);
    curl_setopt($ch, CURLOPT_USERPWD, $login);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

    $gettwit = curl_exec($ch);

    $twitters = new SimpleXMLElement($gettwit);

    $this->twitters = $twitters;
    var_dump( $twitters);

    this the error.. in var_dump()

    object(SimpleXMLElement)#187 (1) {
    [“error”]=>
    string(37) “Basic authentication is not supported”
    }

    maybe the api change, your update the script ?

    Reply
  19. Yanny

    I don’t know if it’s just me, I get this error:
    Fatal error: Uncaught exception ‘Exception’ with message ‘String could not be parsed as XML’ in …
    Pointing to this line: $twitters = new SimpleXMLElement($gettwit);

    I haven’t changed the code except for username and password, so anyone has an idea why this error?

    Reply
Add Comment Register



Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>