Skip to content

Instantly share code, notes, and snippets.

@matherton
Created October 31, 2011 14:29
Show Gist options
  • Save matherton/1327610 to your computer and use it in GitHub Desktop.
Save matherton/1327610 to your computer and use it in GitHub Desktop.
PHP file that parses twitter feed
/*basic styles to display tweets by Mark Atherton*/
ul.tweets {
list-style-type: none;
background-color: #fbfbde;
width: 300px;
padding: 0.25em 1.5em 0.25em 1.5em;
}
ul.tweets h5{
font-size: 1.15em;
}
ul.tweets blockquote {
position: relative;
right: -8em;
}
ul.tweets a {
text-decoration: none;
color: #0099CC;
}
p.time {
font-size: smaller;
color: #999;
}
<html>
<header>
<link href="twitter.css" rel="stylesheet" type="text/css">
</header>
<body>
<?php
$doc = new DOMDocument();
$twuserid = 'your user ID';
# Load the rss document, edit this line to include The Stages username or user ID
if($doc->load('http://twitter.com/statuses/user_timeline/'.$twuserid.'.rss')) {
# specify the number of tweets to display, max is 20
$max_tweets = 2;
echo "<ul class=\"tweets\">\n";
echo "<h5>Recent Tweet</h5>";
$i = 1;
foreach ($doc->getElementsByTagName('item') as $node) {
# fetch the title from the RSS feed.
# Note: 'pubDate' and 'link' are also useful (I use them in the sidebar of this blog)
$tweet = $node->getElementsByTagName('title')->item(0)->nodeValue;
# added to display the time of the tweet
$time = $node->getElementsByTagName('pubDate')->item(0)->nodeValue;
$timeparse = explode(" ", $time);
# the title of each tweet starts with "username: " which I want to remove
$tweet = substr($tweet, stripos($tweet, ':') + 1);
# OPTIONAL: turn URLs into links
$tweet = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $tweet);
# OPTIONAL: turn @replies into links
$tweet = preg_replace("/@([0-9a-zA-Z]+)/", "<a href=\"http://twitter.com/$1\">@$1</a>", $tweet);
echo "<li><p>".$tweet."</p></li>\n";
echo "<li><p class=\"time\">".$timeparse[2]."&nbsp;".$timeparse[1]."&nbsp;".$timeparse[4].", via web</p></li>";
echo "<hr size=\"1px\" width=\"90%\" align=\"center\" noshade>";
if ($i++ >= $max_tweets)
break;
}
echo "<blockquote><a href=\"https://twitter.com/#!/TheStage\">FOLLOW US&nbsp;&nbsp;<img src=\"twitter-arrow.png\" width=\"30\" height=\"30\" align=\"absmiddle\"></a></blockquote>";
echo "</ul>\n";
}
?>
</body>
</html>
@matherton
Copy link
Author

All you need to do is place your twitter ID in between the quotes to display your feeds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment