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.
- 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.
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.
/*Main Feed - User 1*/
$consumer_key_1 = '';//Add your Twitter Consumer Key here
$consumer_secret_1 = '';//Add your Twitter Consumer Secret here
$user_token_1 = '';//Add your Twitter User Token here
$user_secret_1 ='';//Add your Twitter User Secret here
/*Secondary Feed - User 2*/
$consumer_key_2 = '';//Add your Twitter Consumer Key here
$consumer_secret_2 = '';//Add your Twitter Consumer Secret here
$user_token_2 = '';//Add your Twitter User Token here
$user_secret_2 ='';//Add your Twitter User Secret here
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.
include 'twitter/display-tweets.php';//Include the display-tweets file- Ensure that you have the correct path
display_tweets('user1', 'user2','default', 10, 20);//Function to display your tweets
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.
(String) Required Your main Twitter screen name (username)
(String) Required Your Secondary Twitter screen name (username)
(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
(int) Optional How many tweets you want to display. The default is 10.
(int) Optional How many minutes to sync with the Twitter API. The default is 60 minutes.
The default will display your latest 10 tweets for both twitter accounts and sync every 60 minutes (1 Hour).
<?php display_tweets('user1', 'user2','default', 10, 60);?>
To display 20 tweets from both users with the time since tweet option and sync every 30 minutes.
<?php display_tweets('user1', 'user2','time_since', 20, 30);?>
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.
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.