Adding Twitter to your website is something that quite a lot of people do, and why not? One of the most common reasons is that users of your site can follow you via Twitter, and then they’l have somewhere to go (and you have somewhere to broadcast updates) should your site be down for whatever reason.

Important Notes

First there are some important things to remember about using Twitter’s API. First is that there is a limit to how many times you can request information from the API within a hour. This limit is 150 requests & it resets every hour. The easiest way to get around the limit is to cache the information in some way, generally to a file.

Making A Connection

I am going to use cURL to connect to Twitter. I have found that it is the most reliable & arguably the fastest method of retrieving API data from Twitter. You could just a easily use file_get_contents() as long as your server has URL file access enabled on fopen, but I’m going to stick with cURL.

Our connection looks like this:

//initialize a new curl resource
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://twitter.com/statuses/user_timeline/twitterusername.json?count=10');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
curl_close($ch);

if($content === FALSE) {
	//Content couldn't be retrieved... Do something
} else {
	//Content was retrieved do something with it.
}

This creates a connection to Twitter and places all the data retrieved into the $content variable. Just remember you need to change twitterusername to your Twitter username. Also you can change the number after count to get more (up to 200) or less tweets.

Decode The JSON

The data will be in json format, although you can get the data in XML or RSS formats too. If you do not have PHP5 you will not be able to decode json using json_decode(), you could try using the JSON class found here. To use it, save the file as a PHP file (something like json.class.php) then use something like the following:

//include the JSON class
include('json.class.php');
$json = new services_JSON();
//Assume the json is held in $content
$decoded = $json->decode($content);

If you have PHP5 then all you need to do is this:

$decoded = json_decode($content);

Output The Data

Now we have decoded the information into an object/array we can output the data. Here is an example of some code you could use, as it all depends on what data you want to output from the Tweets:

$content = json_decode($content);

foreach($content as $tweet) {
	echo '<p>'.$tweet->text.'</p>';
}

That would output the status text of each tweet in paragraphs. There is a lot of other data held along with it, I can’t list it all as it would be quite a large list. If you want to see what data you can do this:

//Place this before the foreach in the previous code snippet
print '<pre>';
print_r($content);
print '</pre>';

This will list out a nicely formatted list of all the data held in $content. Then you just have to decide what to do with the data, but that I leave to you.

Final Notes

There are a few last things to note about using the Twitter API. First is that I wouldn’t start playing around with the Twitter API until you have a decent knowledge of PHP & have learned at least a little about Object Oriented Programming. Second is that the URL I’ve used has retweets removed from it by Twitter. Third is that due to retweets being removed there is a quirk where the amount of tweets you request is not correct. If you request the 10 latest Tweets and 3 of them were retweets you will only get 7 back. Other than merging retweets back onto your tweets (which is complicated & requires authentication) there doesn’t seem to be anything that can be done about it. One thing I do want to say is that this is my experience, I could be wrong but so far I can’t find any other reason for the missing tweets I’ve experienced.

I think that’s about it. If you have any questions, or need any help let me know in the comments. If you think a downloadable example would help, let me know & I’ll see if I can knock one up. ;)