Adding a twitter feed on a high traffic website

The previous post from me regarding Twitter to place a twitter feed on your site is a fantastically quick way to display your tweets on a page, however it will only be successful on low traffic sites or if that feed is only on a single page of a site. If you had your feed in a sidebar for example that displayed on every page of the site and/or the site gets alot of high traffic, Twitters “Rate Limit” will come into play, only giving you 150 requests an hour. Depending on how your using this and how many people view your site, those requests can diminish very quickly.

So with that in mind I thought I’d give you a few more options, both a little more extensive than the option linked above, but very secure and will allow you to bypass the rate limit.

By using Twitters API and OAuth, you are able to authenticate the use of your account and reading from it. To do this, it does require an access token, which is generated from your account by creating an application (very similar to facebook apps to enable the same thing, however twitters is very simple compared). To do this, login to your twitter account at http://dev.twitter.com and in the drop down under your account when logged in, click on “My Applications”. Click “Create a new application” and you’ll be met with a very simple process of naming and describing the app, and supplying the URL that it will be used on. The callback URL is optional, for what we are using it for in this example, it is not required. Agree to their terms and create away. That easy. On the screen you are presented with, scroll to the bottom and click “Create my access token”. This is the information you will require.

Using the PHP method mentioned on twitter and the OAuth/Twitter library, this is then done all in PHP using the following code:

define('CONSUMER_KEY', '##YOUR_CONSUMER_KEY##');
define('CONSUMER_SECRET', '##YOUR_CONSUMER_SECRET##');
define('OAUTH_TOKEN', '##YOUR_OAUTH_TOKEN##');
define('OAUTH_TOKEN_SECRET', '##YOUR_OAUTH_TOKEN_SECRET##');
function twitterAPI() {
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET);
return $connection;
}
$connection = twitterAPI();
$content = $connection->get("statuses/home_timeline");

$content will then contain your tweets information like the other example I posted up, which you can then display out however you need it to.

Alternatively if you, for some reason, don’t have access to the account you need to be able to create the access token and obtain the other details (say for example you wanted to display your favourite sportsmans tweets on your site), you can use Lupo Montero’s method of fetching tweets and displaying them (or his plugin that he has written) which is a more in-depth version of my method, but includes ajax caching to reduce the rate limit calls and also has the use of the “tweeted on” time/date.

Leave a Reply

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