Primarily the focus on 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 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. For WordPress users you should use WordPress – Display your Cached Tweets using PHP and OAuth
- You should already have a consumer key, consumer secret, access token and access token secret.
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.
Download the Source Files
Last Updated: 14th March 2013
Download the source files from below:
Unzip and copy the folder tweets 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 API.
cacert.pem enables you to access the twitter 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 display-tweets.php in the twitter folder and navigate to lines 7-10. Add your consumer key, consumer secret, user token and user secret, like in the example below. Save and close.
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => '',//Add your Twitter Consumer Key here
'consumer_secret' => '',//Add your Twitter Consumer Secret here
'user_token' => '',//Add your Twitter User Token here
'user_secret' => ''//Add your Twitter User Secret here
Calling the function
Now open index.php in the tweets folder and add your twitter username to Line 14 in place of YOUR_TWITTER_ID. Save and close.
include 'twitter/display-tweets.php';//Include the display-tweets file- Ensure that you have the correct path
display_tweets('','YOUR_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 3 parameters, examples below.
(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
(String) Required Your Twitter screen name (username)
(int) Optional How many tweets you want to display. The default is 10.
The default will display your latest 10 tweets.
<?php display_tweets('','YOUR TWITTER ID');?>
To display 20 tweets with the time since tweet option.
<?php display_tweets('time_since','YOUR TWITTER ID',20);?>
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.