TwitterFetcher fetches Twitter data via the provided Twitter username, and supports the following options:
- specify how many tweets to fetch
- specify whether tweets should be returned as JSON or XML data
- an optional cache file which allows local retrieval of tweets to reduce the number of outbound Twitter connections being made by your site
- a "cacheduration" parameter which dictates how often this cache file should be regenerated
- specify whether Twitter links embedded in tweets should be automatically formatted into clickable links
- support for multiple Twitter feeds
- a human friendly "created at" string that is attached to the object returned in the format of "minutes/days/hours ago", seen frequently on various sites that display recent tweets
- set a cutoff number of days to fetch tweets for
- consumerKey (hash, required): your Twitter consumer key
- consumerSecret (hash, required): your Twitter consumer secret
- accessToken (hash, required): your access token for oAuth authentication
- accessTokenSecret (hash, required): your access token secret for oAuth authentication
- count (int, default = 1): number of tweets to fetch, setting this to 0 will fetch all tweets for the specified user
- usecache (true/false, default = true): whether or not to use a local cache file, which needs to be located at application/cache/twitterstatus.[format], where "format" is either "xml" or "json", and writable by the web server. For first time use this file can be blank, it will be populated after your cacheduration time has elapsed
- cachefile (provide custom cache file name to replace the default twitterstatus.[format] with [cachefile].[format])
- cacheduration (int, default = 5, only relevant if "usecache" is set): regenerate the local cache file if it is older than this value, in minutes
- createlinks (true/false, default = true): whether or not to convert Twitter links embedded in tweets into clickable links
- numdays (int, no default): only fetch tweets newer than this number of days.
<?php
$tweets = $this->twitterfetcher->getTweets(array(
'consumerKey' => '<your consumer key>',
'consumerSecret' => '<your consumer secret>',
'accessToken' => '<your access token>',
'accessTokenSecret' => '<your access token secret>',
'usecache' => true,
'count' => 0,
'numdays' => 30
));
print_r($tweets);
?>
In the above example, the variables "tweets" will be an array consisting of multiple tweets for when count is some value other than 1, or a single tweet object if it is set to 1. A non-standard property will be attached to the object called "elapsedtime" which is a human friendly elapsed time string in the format of "minutes/days/hours ago"
This spark requires the use of Twitter api keys for authentication using oAuth. To get these you need to create your Twitter application from the Twitter developer site. Once you have done so, you will need to generate an access token, and note the consumer key, consumer secret, access token, and access token secret. You will need to provide these hashes in the initilization (see "usage example", above)
1.3.0
- Twitter has disabled API version 1.0, this update supports API version 1.1. Version 1.1 requires authentication for most requests, please see the getting the api keys section for how get the required keys - thanks gamerlv for all of your help here!
1.2.1
- bug fix to handle multiple error types - thanks ramirors for this contribution!
1.2.0
- added support for creating links out of mentions and hashtags - thanks nicekiwi for this contribution!
1.1.0
- don't generate an error if twitter feed does not exist
- custom cachefile variable support
- error check if cache file is writable, not if cache exists
- automatically generate cache the first time if it doesn't exist
- bump CodeIgniter compatibility to 2.1
1.0.4
- fixed a bug that prevented page renders when Twitter REST request returns an empty array
1.0.3
- use the URL scheme documented in the Twitter API documentation, thanks to subdesign for pointing out this flaw!