Routines for reading a user's Twitter timeline and rendering results to HTML5 markup.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.

PHP Twitter utility classes

PHP classes for reading a user's timeline and rendering the results to valid HTML5 markup. Built against the current (as of March 2014) Twitter REST API v1.1 which has now superseded the disabled v1.0 API.


  • PHP 5.5 (using Generators for working over timelines).
  • cURL for HTTP calls.


Allows reading of a user's timeline via the REST API v1.1 GET statuses/user_timeline method and parsing the returned JSON tweet data into usable PHP array structures.

A basic usage example:

$userTimeline = new Twitter\UserTimeline(


$fetchCount = 0;

foreach ($userTimeline->resultList() as $resultItem) {

		[ID] => 448276129489506304 // 64bit integer
		[created] => 1395712332 // unix timestamp
		[userID] => 26228734
		[userFullName] => Peter Mescalchin
		[userScreenName] => magnetikonline
		[text] => Packaged up my PHP classes on GitHub for reading Twitter user timelines via the v1.1 API and marking up to nice HTML
		[replyToID] => // note: false if NOT a reply
		[replyToUserID] => // note: false if NOT a reply
		[replyToUserScreenName] => // note: false if NOT a reply
		[retweetCreated] => // note: false if NOT a retweet
		[entityList] => Array
				[0] => Array
						[type] => url
						[indices] => Array
								[0] => 117
								[1] => 140

						[text] =>
						[url] =>



	if ($fetchCount++ > 5) {
  • In order to make OAuth 1.0a API requests to Twitter you need a set of API access tokens. These can be generated against your Twitter user account at
  • The Twitter\UserTimeline()->resultList() method has been implemented as a PHP generator to lazy load the Twitter timeline in batches (up to a current maximum of 3200 tweets).
    • Batch fetch size can be controlled via the Twitter\UserTimeline()->setFetchBatchSize(SIZE) method (defaulting to 10 items).
  • Twitter\UserTimeline()->resultList() accepts an optional $sinceTweetID parameter, which will tell the API to only fetch tweets more recent than the given tweet ID.
  • By default the API will return classic 140 character tweet responses, automatically truncating longer 280 character tweets to maintain backward compatibility.
    • To return full length tweets, use the Twitter\UserTimeline()->setExtendedTweetMode() method.
  • API v1.1 introduced the concept of entities for Tweets which gives a collection of external resources associated to a tweet. Types returned here will be one of:
    • hashtag.
    • url.
    • user (user mention).
    • media (Twitter photo upload).
  • The OAuth 1.0a routines used internally have been bundled up into a set of fairly easy to use private functions which should make it simple to extend this with further API method calls if desired. The Twitter API documentation provides a step by step breakdown to the OAuth request generation process required.


Takes a data structure emitted by Twitter\UserTimeline()->resultList() above and generates a valid HTML5 representation of the tweet.

Simple example:

$resultItem = $userTimeline->resultList()->current();
$HTMLMarkup = new Twitter\HTMLMarkup();


... will produce (wrapped for readability):

	Packaged up my PHP classes on GitHub for reading Twitter user timelines via the v1.1 API
	and marking up to nice HTML
	<a class="url"
  • Twitter\HTMLMarkup->execute() uses the returned tweet entities to generate anchor tags within the content. Each anchor will have a class assigned of hashtag, url, user and media.
  • Anchor types of hashtag and user will have their respective text wrapped in <span class="text"> elements to allow removal of anchor hover underlines for the preceding @ and # characters via CSS to match default Twitter styling. Example:
    • <a class="hashtag" href="">#<span class="text">hashtag</span></a>
    • <a class="user" href="">@<span class="text">username</span></a>
  • Displayed truncation of long URLs can be controlled with the Twitter\HTMLMarkup->setURLMaxDisplayLength() method (default of 20 characters). Set false to disable truncation.


See the provided example.php for a demo of fetching a total of five tweets, emitting the PHP array structure of each and marking up the result as HTML5.