What do all of the many Twitter mashups hanging around on the internet have in common? Broken avatars! At the rate users upload new avatars every site will at some point have outdated profile information. I have come up with a simple and elegant method of keeping profiles up to date with very little wasted computer cycles.
The basic idea is to bind an event handler onto JavaScript error events with @jQuery. This handler will perform two actions. Fist it will replace the image source with a temporary static link to the users avatar so the visitor will see a working image. Second the handler will ping the server with the screen_name of the missing avatar so the persistent storage can be updated.
In your HTML load jQuery and use selectors to find all image elements with a class of twitter-avatar. The error handler bound to all the selected image elements updates the image source and pings the server.
The second file is in PHP and uses TwitterOAuth but can be in any programming language. It catches POST requests with a screen_name parameter and polls Twitter for updated user information to save to the database.
How are you keeping your database of user avatars up to date?
The basic idea is to bind an event handler onto JavaScript error events with @jQuery. This handler will perform two actions. Fist it will replace the image source with a temporary static link to the users avatar so the visitor will see a working image. Second the handler will ping the server with the screen_name of the missing avatar so the persistent storage can be updated.
In your HTML load jQuery and use selectors to find all image elements with a class of twitter-avatar. The error handler bound to all the selected image elements updates the image source and pings the server.
The second file is in PHP and uses TwitterOAuth but can be in any programming language. It catches POST requests with a screen_name parameter and polls Twitter for updated user information to save to the database.
How are you keeping your database of user avatars up to date?
great contribution abraham, thanks!
ReplyDeleteGreat post. I was banging my head on the wall thinking how to go around with this problem and this solution is so simple.
ReplyDeleteThank you Abraham!
Would there be any changes in your Twitter API client script? I see on Twitter @support there will be some changes on API this weekend.
Regards.
Ado: The changes this weekend might have an effect on TwitterOAuth if you are running 32bit PHP.
ReplyDeleteGreat post. I was banging my head on the wall thinking how to go around with this problem and this solution is so simple.
ReplyDeleteThank you Abraham!
Would there be any changes in your Twitter API client script? I see on Twitter @support there will be some changes on API this weekend.
Regards.
great contribution abraham, thanks!
ReplyDelete