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


by in Tutorials, Web Development


Twitter Feed BrokenWith a little bit of work, we can get your Twitter feed back 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 API’s out there, but we’re using twitter-api-php for its simplicity. Make sure to download this API wrapper and add to your working directory. When you open the twitter-feed-parser.php file, you will see some settings at the top:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 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, 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 webpages can leverage this new twitter feed parser. Something as simple as the below should do the trick:

1
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.

Please, rate this post:

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5.00)
Loading...

Discussion (2)


  • Peter Booker

    It is great to see people offering solutions after the old Twitter API shutdown.

    For those who don’t want to have to create a Twitter App manually, I have created a WordPress plugin which deals with all the Twitter API business for you. You can find it here: http://wordpress.org/plugins/kebo-twitter-feed/

    Reply

  • Jeff Hart

    Thanks for writing this post, Dario! I recently had a client contact me regarding his broken Twitter feed and this fix was quick and easy.

    Reply