Web Development

Your Twitter Feed Is Broken! New API v1.1 With OAuth

Twitter Broken

With a little bit of work, we can get your Twitter feedback in order. As of June 11 of this year, Twitter has enforced authentication of all API endpoints using OAuth. How were we supposed to know this? It was actually announced back in September 2012, oops! But seriously, it's not really that bad and this post will provide a custom PHP solution to get you up and running in no time.

If you would prefer, you can always use the new embedded timelines widget directly from Twitter or one of the many WordPress plugins for more options such as re-tweeting.

For this example, we're going for a simple text feed with some basic links in the output. The source files are below.

Download Source Files

We only have some basic steps to get your Twitter feed up and running with the new API v1.1 authentication.

Step 1 - Set Up Your Twitter App

The first step is to create an app with Twitter. It's pretty straightforward once you visit https://dev.twitter.com/apps where you will be provided with all the OAuth settings needed. Just enter the Name, Description, and Website of your new application details. After you accept the terms and create your application, you will see a button at the bottom of the app settings to create your access tokens.

Step 2 - Configure The PHP Twitter Feed Parser

The next step is to download the source files and upload the two files to your server. There are quite a few APIs out there, but we're using twitter-api-php for its simplicity. Make sure to download this API wrapper and add it to your working directory. When you open the twitter-feed-parser.php file, you will see some settings at the top:

// Your Twitter App Settings
$access_token = 'Add_Your_Access_Token';
$access_token_secret = 'Add_Your_Access_Token_Secret';
$consumer_key = 'Add_Your_Consumer_Key';
$consumer_secret = 'Add_Your_Consumer_Secret';

// Some variables
$twitter_username = 'zadroweb';
$number_tweets = 5; // How many tweets to display? max 20
$ignore_replies = true; // Should we ignore replies?
$twitter_caching = true; // You can change to false for some reason
$twitter_cache_time = 60*60; // 1 Hour
$twitter_cache_file = 'tweets.txt'; // Check your permissions
$twitter_debug = false; // Set to "true" to see all returned values

Go ahead and add your Twitter App settings in the downloaded twitter-feed-parser.php file. You have some very basic options (variables) that you can set. They are pretty self-explanatory. I've also included a caching option, that should get created automatically on your server. If not, add a "tweets.txt" file in the same directory and make sure to adjust the permissions accordingly for writing, typically using chmod 755.

Caching is just great practice, whether pulling from Twitter, Flickr, or really anything using a feed because of rate-limiting and more importantly, the speed of your website. And finally, the output will have the familiar "about 1 day ago" Twitter-style time stamp.

Step 3 - Add The Twitter Feed To Your Website

All that's left to do is to include this file for use in your website or web page. I'm guessing you have a footer include file, or maybe a sidebar, that you use throughout your site. It's best to add this to one of those files so that all your web pages can leverage this new Twitter feed parser. Something as simple as the below should do the trick:

include($_SERVER['DOCUMENT_ROOT'].'/your_folder/twitter-feed-parser.php');

That's it!

Have fun with this simple script and feel free to use it under the MIT license, which means do whatever you want with it. Enjoy and feel free to add any suggestions or comments below. Oh, and please share. Thanks for reading.