Primarily the focus on this tutorial is to display your cached tweets for 2 users using PHP and OAuth on your website. We will be accessing the secure Twitter 1.1 API with PHP by parsing both json files and merging them into a single file to cache and store on your server. This is a follow up from Display Your Cached Tweets Using PHP and OAuth which is recommended for a single twitter user.

Prequisites

  • You should already have a consumer key, consumer secret, access token and access token secret for both of your twitter accounts.
    If not please go to http://www.worldoweb.co.uk/2012/create-a-twitter-app-for-the-new-api for a little guide into 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 are beneficial but not essential.
  • This tutorial uses the tmhOAth script by Matt Harris, included in the source files and regularly updated.

Download the Source Files

Last Updated: 30th October 2013
Download the source files from below:
If you are upgrading from 1.1 or below please delete the contents of the cache folder.

Download Display Tweets

Unzip and copy the folder twitter and all it’s contents to your server.
tmhOAuth.php is a PHP authentication script to allow you to make a secure call to the twitter 1.1 API.
cacert.pem enables you to access the API using SSL. You can also download a copy from the following: http://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 twitter/includes folder and navigate to lines 6-9 for the main account and 13-16 for the secondary account. Add your consumer key, consumer secret, user token and user secret, like in the example below. Save and close. You can also change whether you want to include retweets and/or exclude replies from your feed by changing the respective values to either True or False. Please note replies and retweets will count towards the maximum number you request, unfortunately I cannot change it due to the Twitter API. The only suggestion is to set max_tweets to a higher amount when calling the display_tweets function.

Calling the function

Now open index.php add your twitter user-names to Line 24 in place of user1 and user2, don’t worry about the other settings just yet so leave them at their defaults. Save and close.

Open the file in you web browser to test that it is running OK. Now you can change the parameters in the display_tweets(); function to suit your requirements.

$user1

(String) Required Your main Twitter screen name (username)

$user2

(String) Required Your Secondary Twitter screen name (username)

$style

(String) Optional Displays the date or time since format. Leave 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 you want to display. The default is 10.

$time

(int) Optional How many minutes to sync with the Twitter API. The default is 60 minutes.

Examples

The default will display your latest 10 tweets for both twitter accounts and sync every 60 minutes (1 Hour).

To display 20 tweets from both users with the time since tweet option and sync every 30 minutes.

Styling our Tweets

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

Tweet Themes

Conclusion

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