A simple php class to fetch twitter statuses
Posted on May 24, 2009 by Phil 5 comments
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
Related Posts
No related posts.
5 Responses So Far
-
Pingback: Daily News About Twitter : A few links about Twitter - Sunday, 24 May 2009 05:12
-
-
Lisham March 26, 2011 at 4:33 pm
Thanks a lot
it works great -
Ishani Vardhan September 13, 2011 at 1:08 pm
How do I create a PHP class which can send or pull tweets from any given user using OAuth Twitter API library?





Hey great script,
I was wondering if you have twitter.php for php 4.4.9
my school hasn’t upgraded to 5.0
here is the error I get
it works on my person site perfectly fine.
Parse error: parse error, expecting `T_OLD_FUNCTION’ or `T_FUNCTION’ or `T_VAR’ or `’}” in /Twitter.php on line 5