If you do read my blog, you would have noticed a post on the retirement of Twitter API v1, the launch of v1.1, and the subsequent [new] way of retrieveing the latest tweets of a given user.
This post was all about getting a twitter user timeline without having to dig deep into authentication. Today, I’m going to show you how to do the same thing with authentication.
What is OAuth?
Twitter uses OAuth to provide authorized access to its API, giving its users a secure way to connect their Twitter accounts with 3rd party apps without having to share their passwords, and providing them in the same time with a wide range of libraries.
In order to get started, let’s first connect our twitter account to the Twitter Developers interface and create a new application.
Give it a name, a description, fill in your website url, leave the callback url blank, tick the terms and conditions checkbox, fill in the captcha, and create your app.
Once created, request your access tokens by clicking on the blue button at the bottom. This is basically all you need to access the API with authentication. And if you do take a look at the details of the app, you’ll understand you’ll be needing all four tokens present on the page (ie the consumer key, the consumer secret, the access token, and the access token secret).
TwitterOAuth PHP Library
Now, you’ll need a php library to work with Twitter’s OAuth API. If you google twitteroauth, you’ll find quite a few of libraries to use. The one we are going to use here is Abraham’s.
Download all files, and copy the twitteroauth folder into your project folder, as you won’t be needing the rest of the files.
Start with a blank html5-ready index.php file such as the following one:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title></title> <meta name="description" content=""> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> </body> </html>
In the body, start coding your php. We are going to require twitteroauth.php (and its mate OAuth.php) we have just donwloaded. Note that you don’t need to call both of them, twitterouath.php is enough.
Create a few variables to hold you app’s consumer key, consumer secret, access token, and access token secret. Create as many parameter variables as you wish. To give you a head start, we are going to use GET statuses/user_timeline and all parameters are listed in there.
For our example, I created a couple of variables sunch as $nb_of_tweets (which is going to do – well – exactly what it says on the tin), and an $include_rts to include or exclude the RTs from your timeline.
$consumer_key = 'xxxxxxxxxxxxxxxxxxxxxx'; $consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; $access_token = 'xxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; $access_token_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; $nb_of_tweets = 5; $include_rts = false;
Note that RTs can be excluded with a ‘false’ value, but they will still count toward both the maximal length of the timeline and the slice selected by the count parameter. So it says on the user_timeline spec page.
Connect to the API using the get(‘statuses/user_timeline’) command, and run a loop to display your tweets in an unordered list.
Kudos to Kevin who commented below and added his two cents to helps us improve hyperlink display.
And a shit lot more kudos to him again as he also fixed our @mentions and #hashtags links, as I kindly asked him to. [Brilliant mate!]
Full code looks a lot like:
<?php require_once("twitteroauth/twitteroauth.php"); $consumer_key = 'xxxxxxxxxxxxxxxxxxxxxx'; // Put your consumer key here $consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // Put your consumer secret here $access_token = 'xxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // Put your access token here $access_token_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // Put your access token secret here $nb_of_tweets = 5; // Nb of tweets to be displayed $include_rts = false; // true to include RT's or false to exclude them $connection = new TwitterOAuth($consumer_key,$consumer_secret,$access_token,$access_token_secret); $tweets = $connection->get('statuses/user_timeline', array('count' => $nb_of_tweets, 'include_rts' => $include_rts)); ?> <ul> <?php foreach ($tweets as $key => $tweet): ?> <li><?php //links $tweet = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a target='_blank' href=\"\\0\">\\0</a>", $tweet->text); //# $tweet = preg_replace('/(^|\s)#(\w*[a-zA-Z_]+\w*)/', '\1<a target="_blank" href="https://twitter.com/search?q=%23\2&src=hash">#\2</a>', $tweet); //@ $tweet = preg_replace('/[@]+([A-Za-z0-9-_]+)/', '<a target="_blank" href="http://twitter.com/$1" target="_blank">@$1</a>', $tweet ); echo $tweet; ?></li> <?php endforeach ?> </ul>
Refresh your page, you should be getting your latest tweets listed nicely in an unordered list.
Note that GET statuses/user_timeline has got limitations. For example, the maximum number of requests allotted per user is currently set to 180 per 15 minute window. Therefore, if you do get a lot of trafic on your site, it would be wise to optimize all this by setting some kind of cache and avoid reaching the 180 request limit.
For people testing this out, make sure you run your test in a local environment (using MAMP, or xampp for windows users).
Most Recent Posts
Special Recent Posts
December 8th, 2013
I gave Shane Osbourne's browser-synchronising testing tool a go and wasn't disappointed. I had taken[...]
October 31st, 2013
I have been wanting to give Jekyll a go for ages, and when I saw Seth's post last night (although I'[...]