Version 1.5.3
Framework for easy use of Twitter's API with PHP
Requires twitteroauth by abraham
Twitterfeed-PHP introduces a class called Twitterfeed that allows you to easily make requests to twitter and get various tweet information from feeds.
You should have three different php files in a directory called twitterfeed:
- twitterfeed.php - contains the Twitterfeed class
- twitteroauth.php - from twitteroauth
- OAuth.php - from twitteroauth
You'll need to define four constants that hold your four keys in twitterfeed.php:
API_KEYAPI_SECRETACCESS_KEYACCESS_SECRET
You can get these by creating a twitter application via the Twitter Application Management site.
Throw this somewhere in your file before the twitterfeed functions to define the class. A good place is the header.
<?php require_once('twitterfeed/twitterfeed.php'); ?>
To set up a list of tweets, you need to create a Twitterfeed object. It's a good idea to make it global if you need to use the class in various files.
<?php $twitter = new Twitterfeed('user', 'twitter', 5); ?>
When wanting to access individual tweets, use the following loop. Some functions only work within this loop.
<?php foreach ($twitter->tweets as $twitter->tweet) : ?>
...
<?php endforeach; ?>
And that's it!
Here are some pages you might want to look into - these will give you the attributes to use.
Here's a basic example showing a list of Twitter's latest 5 tweets:
<html>
<body>
<?php require_once('twitterfeed/twitterfeed.php'); ?>
<?php $twitter = new Twitterfeed('user', 'twitter', 5); ?>
<ul>
<?php foreach ($twitter->tweets as $twitter->tweet) : ?>
<li>
<h4><?php $twitter->author('name'); ?>:</h4>
<p><?php $twitter->tweetHTML(); ?></p>
</li>
<?php endforeach; ?>
</ul>
</body>
</html>
There is a detailed example included in the repository. It uses header.php to display feed headers and tweets.php to display feed tweets.
Note - the argument echo is set to true by default. This means it will output the value by default. Set this to false if you don't want this happening.
Constructor for the Twitterfeed class
The type can be the following:
"user"-valuemust be the username"search"-valuemust be search query
Returns: Boolean
Returns whether the Twitterfeed has any tweets in it.
Returns: Boolean
Returns whether the Twitterfeed matches that type.
Returns: Mixed
Outputs the Twitterfeed user's information.
Returns: Mixed
Outputs the current Twitterfeed's search query only if the type is a search.
Returns: Mixed
Outputs the Twitterfeed user's avatar URL.
Returns: Mixed
Outputs the Twitterfeed user's URL.
Returns: Mixed
Outputs the current tweet's information.
Returns: Mixed
Outputs the tweet's text without any tags.
Returns: Mixed
Outputs the tweet's text with tags. If any of the variables are set to false, they are wrapped within a span element instead. URLs are given the class url, hashtags given the class hashtag and mentions given the class mention. If mediaLinks is set to false, it will not show links that link to media entities.
Returns: Mixed
Outputs the current tweet's URL.
Returns: Mixed
Same as user(), but returns the current tweet's author instead.
Returns: Mixed
Outputs the current tweet author's avatar URL.
Returns: Mixed
Outputs the current tweet author's URL.
Returns: Boolean
Returns whether the current tweet is a retweet.
Returns: Boolean
Returns if the current tweet is a reply.
Returns: Mixed
Outputs the screen name of the user the current tweet was in reply to.
Returns: Boolean
Returns if the current tweet has any media entities attached to it. Note that animated GIFs do not fall under media entities.
Returns: Mixed
Returns or outputs media entity data associated with the current tweet.
- If nothing is specified, return an array of the media entities
- If an index is given, output the media_url of the given media entities
- If a property is given, output the property of the given media entity instead