Fetch twitter updates via PHP

Want to display your (or maybe be someone else’s) public timeline using php? Ok, here’s the solution for it:

<?php
$username = “amanjain”;
$rssUrl = “http://twitter.com/statuses/user_timeline/$username.rss”;
$rss = @file_get_contents($rssUrl);

if($rss)
{
$xml = @simplexml_load_string($rss);
if($xml !== false)
{
foreach($xml->channel->item as $tweet)
{
echo $tweet->pubDate.”\n”;
echo substr($tweet->description,10).”\n\n”;
}
}
else
{
echo “Error: RSS file not valid!”;
}
}
else
{
echo “Error: RSS file not found, dude. Username invalid or requires authentication”;
}
?>

Find the script live here: http://blog.amanjain.com/twitter

15 Responses to “Fetch twitter updates via PHP”

Ramankit Singh (January 4th, 2009 at 6:54 am)

nice idea dude….. :)

Aman (January 4th, 2009 at 7:15 am)

Thanks mate..

Ramankit Singh (January 4th, 2009 at 7:15 am)

cool article…………u rock dude….

SeanJA (January 7th, 2009 at 9:52 am)

Alternatively you could use the http://search.twitter.com/search.rss?q=username&@username if you just want things that are from and to that user.

[Web] (January 14th, 2009 at 7:44 pm)

[...] Fetch twitter updates via PHP [...]

James (February 13th, 2009 at 4:22 am)

Thank you for this!!! But I have a problem, i’m very limited in PHP skill, I get this error:

“Parse error: syntax error, unexpected ‘:’ in /home/jamesca1/public_html/test.php on line 3″

Code for simple page with twitter output:

Twitter Test

channel->item as $tweet)
{
echo $tweet->pubDate.”\n”;
echo substr($tweet->description,10).”\n\n”;
}
}
else
{
echo “Error: RSS file not valid!”;
}
}
else
{
echo “Error: RSS file not found, dude. Username invalid or requires authentication”;
}
?>

What am I doing wrong?

Duncan (March 26th, 2009 at 8:46 pm)

The reason you were getting that error is most likely because you have curly quotes – change all the double quotes to ’straight’ quotes

Lil aka XaosQueen (May 21st, 2009 at 11:02 am)

Hi there – GREAT little piece of code (can’t wait to try it!) .. is there a way to get the updates of EVERYONE you are following to be parsed and re-posted via your own website (for my own reading – Twitter is blocked here but I use it to get info for work so they are cutting off their nose to spite their faces!)Thanks so much! …

Twitter Search (May 26th, 2009 at 4:56 pm)

thats great that you are talking about the twitter api,a good example of searching with the twitter api is on twiogle.com because you can search on twitter and google at the same time.

Sea Squirt Genome (August 4th, 2009 at 2:09 am)

hm.. thank you..

Maarten (August 10th, 2009 at 10:34 am)

I have a few questions:

How can I only display the latest 5 tweets?
How can I change the date notation?
How can I add an URL to the tweet?

And thanks for the great script!

Timo (August 18th, 2009 at 11:44 am)

Hello Aman,
this is really a nice little script. Thank you!
Is there a way to display the date in an other format, like you can do with date() or strftime()?

Timo (August 18th, 2009 at 12:25 pm)

I got it!
I edited the date-line (echo $tweet->pubDate.”\n”;) to be something like:
$d = explode(”-”, $date);
$t = explode(”:”, $time);
$pubdate = date(”r”, mktime((int)$t[0], (int)$t[1], (int)$t[2], (int)$d[1], (int)$d[2], (int)$d[0]));
echo date(”d.m.Y, H:i”, strtotime($tweet->pubDate)).”\n”;

Now I’ve only to make the @-links clickable (when you answer someone) and links in tweets. It hink, I’ll use preg_match

Aman (August 31st, 2009 at 1:11 am)

@Maarten: You can use a counter to count 5 tweets for date notation Timo has written a good piece of code :)

Leave your response!

Be nice. Keep it clean. Stay on topic. No spam.