Skip to content
This repository was archived by the owner on Apr 23, 2022. It is now read-only.

niksudan/twitterfeed

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

59 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

twitterfeed

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.

Contents

Setup

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

Implementation

You'll need to define four constants that hold your four keys in twitterfeed.php:

  • API_KEY
  • API_SECRET
  • ACCESS_KEY
  • ACCESS_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.

Examples

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.

Functions

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.


Twitterfeed(String type, String value, Integer limit)

Constructor for the Twitterfeed class

The type can be the following:

  • "user" - value must be the username
  • "search" - value must be search query

hasTweets()

Returns: Boolean

Returns whether the Twitterfeed has any tweets in it.


isType(String type)

Returns: Boolean

Returns whether the Twitterfeed matches that type.


user([String property, Boolean echo])

Returns: Mixed

Outputs the Twitterfeed user's information.


searchQuery([Boolean echo])

Returns: Mixed

Outputs the current Twitterfeed's search query only if the type is a search.


userAvatar([Boolean echo])

Returns: Mixed

Outputs the Twitterfeed user's avatar URL.


userURL([Boolean echo])

Returns: Mixed

Outputs the Twitterfeed user's URL.


Loop Only Functions


tweet([String property, Boolean echo])

Returns: Mixed

Outputs the current tweet's information.


tweetText([Boolean echo])

Returns: Mixed

Outputs the tweet's text without any tags.


tweetHTML([Boolean links, Boolean hashtags, Boolean mentions, Boolean mediaLinks, Boolean echo])

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.


tweetURL([Boolean echo])

Returns: Mixed

Outputs the current tweet's URL.


author([String property, Boolean echo])

Returns: Mixed

Same as user(), but returns the current tweet's author instead.


authorAvatar([Boolean echo])

Returns: Mixed

Outputs the current tweet author's avatar URL.


authorURL([Boolean echo])

Returns: Mixed

Outputs the current tweet author's URL.


isRT()

Returns: Boolean

Returns whether the current tweet is a retweet.


isReply()

Returns: Boolean

Returns if the current tweet is a reply.


inReplyTo([Boolean echo])

Returns: Mixed

Outputs the screen name of the user the current tweet was in reply to.


hasMedia()

Returns: Boolean

Returns if the current tweet has any media entities attached to it. Note that animated GIFs do not fall under media entities.


media([Integer index, String property, Boolean echo])

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

About

🐦 Basic PHP Twitter feed API

Topics

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors