Web Development

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.

Prerequisites

  • You should already have a consumer key, consumer secret, access token and access token secret.
    If not please go to https://www.worldoweb.co.uk/2012/create-a-twitter-app-for-the-new-api 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: https://curl.haxx.se/ca/cacert.pem

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.

<?php 
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);?>

$twitter-id

(String) Required Your Twitter screen name (username)

$style

(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

$max_tweets

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

$time

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

Examples

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.

Conclusion

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.

Share
Published by
Tracy Ridge

Recent Posts

Hot New Web Dev – November 2024

Welcome to Hot Web Dev November 2024, featuring the latest technology and web development news.… Read More

3 weeks ago

Hot New Web Dev – October 2024

Welcome to Hot Web Dev October 2024, featuring the latest technology and web development news.… Read More

2 months ago

New Svelte 5 Guessing Game 2024

In this tutorial, you’ll build a fun and interactive guessing game using Svelte 5, the… Read More

3 months ago

Hot New Web Dev – September 2024

Welcome to Hot Web Dev September 2024, featuring the latest technology and web development news.… Read More

3 months ago

New JavaScript Guessing Game 2024

The JavaScript guessing game tutorial is a simple beginner's project. It features modern JavaScript syntax… Read More

3 months ago

Hot Web Dev – 12 Useful Top Tools of 2023

If you have been following the monthly Hot Web Dev magazine you will find the… Read More

3 months ago