A simple php class to fetch twitter statuses
Redesigning my site recently led me to add a twitter badge in the sidebar showing my most recent tweets. Searching on google for a nice simple php approach to pulling them returned lots of bloated classes and functions that I really didn’t feel happy using so I decided to pick through the twitter API and figure it out for myself. I ended up with a simple class that can be used to access the date and text of a given number of tweets and also the user link.
All you need do is simply create an instance of the class by supplying your username and the number of tweets you would like to retrieve as arguments. The class constructor creates a two dimensional array of the tweets and gives access to the array via the getTweets() method. The array is set up with the keys status and pDate.
Below is an example of how the class is implemented:
<?php
include 'Twitter.php';
$twitter = new Twitter(username, numberOfTweets);
$tweets = $twitter->getTweets();
foreach ($tweets as $tweet) {
echo '<p>' . $tweet['status'] . ' - ' . $tweet['pDate'] . '</p>';
}
?>
The code for the class is here and can be downloaded in a zip file at the end of the post:
<?php
class Twitter {
private $doc;
private $tweets;
private $userLink;
public function __construct($user, $num) {
$this->doc = new DOMDocument();
$this->tweets = array();
if ($this->doc->load('http://twitter.com/statuses/user_timeline/' . $user . '.rss?count=' . $num)) {
$i = 0;
foreach ($this->doc->getElementsByTagName('item') as $node) {
$tweet = array(status => $this->getStatus($node), pDate => $this->getDate($node));
$this->tweets[$i] = $tweet;
if ($i == 0) $this->userLink = $node->getElementsByTagName('link')->item(0)->nodeValue;
$i++;
}
}
}
private function getStatus($node) {
$tweet = $node->getElementsByTagName('title')->item(0)->nodeValue;
#remove user name
$tweet = substr($tweet, stripos($tweet, ':') + 1);
#find urls and convert to links
$tweet = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $tweet);
#link replies
$tweet = preg_replace("/@([0-9a-zA-Z]+)/", "<a href=\"http://twitter.com/$1\">@$1</a>", $tweet);
return $tweet;
}
private function getDate($node) {
$date = $node->getElementsByTagName('pubDate')->item(0)->nodeValue;
#strip the +0000 from the end
return substr($date, 0, strpos($date, '+0000'));
}
public function getTweets() {
return $this->tweets;
}
public function getLink($msg) {
return '<a href="' . $this->userLink .'">' . $msg . '</a>';
}
}
There is also a method to pull the user link via the getLink(message) method where message is the text you would like the link to show, as in follow me.
Download the Twitter class now
I’m no php guru so if you have any suggestions for improvements to my code then please be sure to enter them in the comments section and of course feel free to use the class for your own projects
Posted on May 24th, 2009 under PHP tips. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

May 24th, 2009
[...] A simple php class to fetch twitter statuses [...]