TweetPHP: Display tweets on your website using PHP
If you’ve ever wanted to display your latest Twitter tweets on a website, TweetPHP lets you do that using PHP. TweetPHP is a rewrite of my old Twitter RSS feed parser, which stopped working when Twitter phased out their v1 API.
- Works with Twitter API v1.1
- Tweets are cached to avoid exceeding Twitter’s API request rate limits
- A fallback is provided in case the twitter feed fails to load
- Retweets and @replies can be optionally ignored
- A configuration parameter allows you to specify how many tweets are displayed
- Dates can optionally be displayed in “Twitter style”, e.g. “12 minutes ago”
- You can customize the HTML that wraps your tweets, tweet status and meta information
To interact with Twitter’s API you will need to create an API KEY, which you can create at: https://dev.twitter.com/apps
After creating your API Key you will need to take note of following values: “Consumer key”, “Consumer secret”, “Access token”, “Access token secret”
Those values can be passed as options to the class constructor, along with the Twitter screen name you wish to query:
require_once('TweetPHP.php'); $TweetPHP = new TweetPHP(array( 'consumer_key' => 'xxxxxxxxxxxxxxxxxxxxx', 'consumer_secret' => 'xxxxxxxxxxxxxxxxxxxxx', 'access_token' => 'xxxxxxxxxxxxxxxxxxxxx', 'access_token_secret' => 'xxxxxxxxxxxxxxxxxxxxx', 'twitter_screen_name' => 'yourusername' ));
Then you can display the results like so:
See TweetPHP’s Github for documentation of all options.
Caching is employed because Twitter rate limits how many times their feeds can be accessed per hour.
When the user timeline is first loaded, the resultant HTML list is saved as a text file on your web server. The default location for this file is:
You can change this file path by setting the
cache_file option. For example, to set a path from your root public directory try:
$_SERVER['DOCUMENT_ROOT'] . '/path/to/my/cache/dir/filename.txt'
If you are experiencing problems using the plugin please set the
debug option to
true. This will set PHP’s error reporting level to
E_ALL, and will also display a debugging report.
Here are a few tips to help you solve common configuration issues:
tmhOAuth response code: 0
If your debugging report shows the error “tmhOAuth response code: 0”, you can find out more about this error message in the tmhOAuth github README. Dario also offers this solution: “My server didn’t like the the two __DIR__ constants in the tmhOAuth.php file, hard-coded them and it all works now.”
If you receive the PHP warning “[function.unserialize]: Error at offset 0 of 49 bytes”, it might be the case that you have magic quotes enabled in your PHP configuration.