Recently, Twitter shutdown version 1.0 of their API, completely switching to version 1.1. Among other things, this version of their API requires authentication to retrieve Tweets, even from a public timeline.
As you may have noticed, the sidebar on the right has a few of my recent Tweets, which meant I had to replace my old, simple piece of code with the more complicated version, which you are free to insert into your own site.
Think of it as a gift. From me, to you. And maybe Twitter, as well. Which means you're not sure who to write the thank you note to, are you?
Here is the more complicated version:
This function simply takes a couple of security values provided by Twitter, the screen name of the owner of the desired timeline, and the number of tweets to retrieve. It then outputs an associative array linking the date of the Tweet with the Tweet HTML (links, usernames and hashtags are wrapped in anchor tags).
You'll then be taken to a screen that looks like this:
Don't bother trying to steal the keys you see in the image above, get your own. These ones were taken from the official Twitter documentation, anyway, so they probably don't do anything.
Make a note of the consumer key and consumer secret values.
Add the above function into your code. The first two arguments are the consumer key and consumer secret you made a note of in the last step. The third argument refers to the Twitter screen name, quite possibly your own, of the user you wish to retrieve the Tweets from (in my case, cmuirnz). The fourth argument is the number of recent tweets you wish to retrieve, I believe the maximum is 200. The final argument refers to a file that caches your recent tweets for 1 minute, so you're not making unnecessary requests to Twitter for every page a user visits.
So, you can call it like this (but with your own keys, obviously):
And you've got a simple of array of tweets, which you can insert into your HTML.
At this point, you may be wondering why Twitter's making us jump through hoops with authentication in order to view tweets which are publicly available. Twitter claim it's in order to better understand and manage the needs of their developers.
I, for one, believe they have a much simpler motivation. Obviously, their servers are powered by the kinetic energy of coders like you and I typing out code. The more code we have to write in order to retrieve our tweets from their servers, the less energy they need to siphon from their orbiting death-ray in order to keep their servers going.
As a staunch defender of this planet's safety from fiery sky-death, it is your duty to use the code I have provided, instead of coding it out for yourself. Think of the lives you'll save; you'll be a hero. Good luck.