Created
January 12, 2013 22:10
-
-
Save lylo/4520687 to your computer and use it in GitHub Desktop.
How to display Twitter updates using PHP (and without using curl)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$doc = new DOMDocument(); | |
# load the RSS -- replace 'lylo' with your user of choice | |
if($doc->load('http://twitter.com/statuses/user_timeline/lylo.rss')) { | |
echo "<ul>\n"; | |
# number of <li> elements to display. 20 is the maximum | |
$max_tweets = 10; | |
$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; | |
# 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>". $tweet . "</li>\n"; | |
if($i++ >= $max_tweets) break; | |
} | |
echo "</ul>\n"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment