Skip to main content Jump to list of all articles

Display Your Cached Tweets Using PHP and OAuth

Primarily the focus of this tutorial is to display your cached tweets using PHP and OAuth on your website. We will be accessing the secure Twitter 1.1 API with PHP by parsing and caching the JSON file to store on your server. It may seem like a lot of code just to display your latest tweets on your website but the benefits are that you can style it to your own taste with a little CSS and caching the JSON file will not add extra load on the Twitter API. Check out WOW-Multi-Twitter 2.0 For WordPress users you should use WordPress – Display your Cached Tweets using PHP and OAuth or my new plugin.


  • You should already have a consumer key, consumer secret, access token and access token secret.
    If not please go to for a little guide to creating the Twitter App required for this tutorial.
  • PHP Server with CURL – I have tested it using PHP 5.3+
  • A little knowledge of PHP and CSS is beneficial but not essential.
  • This tutorial uses the tmhOAth script by Matt Harris, included in the source files.

Download the Source Files

Last Updated: 23rd August 2013
Download the source files below:

Unzip and copy the folder tweets and all its contents to your server.
tmhOAuth.php is a PHP authentication script to allow you to make a secure call to the Twitter API.
cacert.pem enables you to access the Twitter API using SSL. You can also download a copy from the following:

Adding Keys and Tokens

Once you have copied the files to your server I suggest that you test the installation before deploying it on your live website. Open up keys.php in the includes folder. Add your consumer key, consumer secret, user token and user secret. You can also change the settings for displaying retweets and exclude replies if you wish. Please note that replies and retweets are filtered from the maximum tweets that are set. If you have lots of retweets and replies set max_tweets to a higher amount, unfortunately, this is due to the Twitter API and not this script! Save and close.

Calling the function

Now open index.php in the tweets folder and add your Twitter username to Line 29 in place of twitter-id. Save and close.

include 'twitter/display-tweets.php';//Include the display-tweets file- Ensure that you have the correct path
display_tweets('twitter-id');//Function to display your tweets

Navigate to index.php in the tweets folder with your browser and you should see your latest Twitter posts. Now you can change the parameters in the display_tweets(); function to suit your requirements.

The display_tweets function takes 4 parameters, examples below.

<?php display_tweets($twitter-id,$style,$max_tweets,$time);?>


(String) Required Your Twitter screen name (username)


(String) Optional Displays the date or time since format. Leave the string empty if not wanting to use a date.
The arguments are:

  • ‘eng_suff’ – Displays 6th November
  • ‘ddmm’ – Displays 06 Nov
  • ‘ddmmyy’ – Displays 06 Nov 2012
  • ‘full_date’ – Displays Tues 06 Nov 2012
  • ‘time_since’ – Displays the time since the tweet in hours, minutes etc.
  • ‘default’ – Displays November 06 2012


(int) Optional How many tweets do you want to display? The default is 10.


(int) Optional How often do you want to fetch the tweets, in minutes from the Twitter API? The default is 60 minutes.


The default will display your latest 10 tweets at 60-minute intervals with no style.

<?php display_tweets('twitter-id');?>

To display 50 tweets at 20-minute intervals using the time since style.

<?php display_tweets('twitter-id','time_since',50,20);?>

Styling our Tweets

There are 4 stylesheets enclosed, 3 of which are different themes and 1 is an HTML5/CSS reset. Feel free to roll your own or use any of the themes as a guide.

Tweet Themes showing cached tweets


If you are pretty new to PHP then you may want to stick with the defaults and then use it as a basis to learn. If you have any problems or recommendations or tweaks I would love to hear them.

16 replies on “Display Your Cached Tweets Using PHP and OAuth”

I have updated the script to a new version. Added a time argument, moved config from main display-tweets.php file to it’s own keys.php file, ability to filter retweets and replies, if twitter API goes down no file is requested and will continue to use previous json file, which should fix ‘about 43 years ago’ bug. Cleaned up duplicate code and managed to shave a few KB compared to previous versions despite the new features. If you encounter any bugs please post here. More info inside source files.

Every now and then I’m getting the message ‘about 43 years ago’ and no tweets displayed. If I delete the cache it goes back to displaying tweets. Any idea what causes this issue?


Did you notice any error messages in the file that was cached? I’ll have a look to see if I can replicate it. If you see it in the meantime email me a screenshot and a the contents of the cached file.

It happened again recently and looking at the content of the cache file I saw:

{“errors”:[{“message”:”Rate limit exceeded”,”code”:88}]}

I’m just in the process of doing that now. I see you solved the ‘about 43 years ago’ bug so hopefully this will solve the problem. Thanks.

All done and working; however, it didn’t work with the new version of the authentication script. I’ve gone back to the older version and it’s working. Any thoughts?

Strange! Did you delete all the old stuff from the previous installation? Could you test the latest version again in a sub folder/ non live part of your web server? Enable PHP errors by adding the following to the top of index.php

ini_set('display_errors', 1);

The error was caused by a bug in the first version which was fixed in the latest release.

If I use the new version of the authentication script the cache file is not created and I get this error:

Warning: file_get_contents(cache/my-id.json) [function.file-get-contents]: failed to open stream: No such file or directory in/htdocs/public/www/tweets/twitter/display-tweets.php on line 64

(I’ve manually updated my ID in the above error message to remove it for this post)

If I then replace the authentication script with the older version it works fine.

I’ve got it working now. I needed to pre-populate the cache and allow one cycle before it started working.

Thank you so much for your help and posting this excellent script.

evanrose On

In your index.php file, there is a missing quote in your display_tweets function where one would enter their username.

Comments are closed.

Discover more from WorldOWeb

Subscribe now to keep reading and get access to the full archive.

Continue reading