Nov
23
2009
A Better Twitter Feed for Your Web Site
I have already posted a tutorial on How to Create a Twitter Feed on Your Web Site and I have been having a lot of emails in regards to people whose hosts do not allow URL file-access. Here is an alternative script that fetches and parses your Twitter RSS feed using a different function. Hopefully it will help out those who can’t use the other script.
The first thing you need to do is get your Twitter RSS feed URL. go to your Profile page and you will see a link in the bottom of your sidebar that says RSS feed. Click on this and then copy the URL. That will be your feed URL.
<ul> <?php $feedURL = "http://twitter.com/statuses/user_timeline/68559295.rss"; // change to your feed URL $doc = new DOMDocument(); $doc->load($feedURL); $arrFeeds = array(); foreach ($doc->getElementsByTagName('item') as $node) { $itemRSS = array ( 'title' => $node->getElementsByTagName('title')->item(0)->nodeValue ); array_push($arrFeeds, $itemRSS); } $limit = 5; for($x=0;$x<$limit;$x++) { $title = str_replace('bavotasan: ', '', $arrFeeds[$x]['title']); $str = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\">\\0</a>", $title); $pattern = '/[#|@][^\s]*/'; preg_match_all($pattern, $str, $matches); foreach($matches[0] as $keyword) { $keyword = str_replace(")","",$keyword); $link = str_replace("#","%23",$keyword); $link = str_replace("@","",$keyword); if(strstr($keyword,"@")) { $search = "<a href=\"http://twitter.com/$link\">$keyword</a>"; } else { $link = urlencode($link); $search = "<a href=\"http://twitter.com/#search?q=$link\" class=\"grey\">$keyword</a>"; } $str = str_replace($keyword, $search, $str); } echo '<li>'.$str.'</li>'; } ?> </ul> |
Twitter Bird icon provided by Loon Design.




Hi I have got this working a treat BUT am trying to access the tweet popst date too but cant fathom it out i had assumed that i just use:
‘pubDate’ => $node->getElementsByTagName(‘pubDate’)->item(2)->nodeValue
in te irem rss array??? and then use $arrFeeds[$x]['pubDate']; in the loop but it doesnt work?? Thanks!!
Hi, Please can you herlp me with this if you know how to??… I have got this working a treat BUT am trying to access the tweet post date too but cant work it out i had assumed that i just use:
‘pubDate’ => $node->getElementsByTagName(‘pubDate’)->item(2)->nodeValue
in the item rss array??? and then use $arrFeeds[$x]['pubDate']; in the loop but it doesnt work?? Thanks!!
I had some troubles getting twitter to work with my website but this gave me some ideas as to how to fix it. Thanks.
Is there any way to remove the name of my twitter account before every post?
Example:
jamestoone: What a nice day
You can add another line like this at the end of the last foreach statement:
Thanx a lot for that php code. It works great. Solved all my tweet-button problems
Deprecated: Function ereg_replace() is deprecated in /Applications/XAMPP/xamppfiles/htdocs/Site/index.php on line 210
is there an alternative?
When i replace erg_replace with preg_replace I get the following error:
Warning: preg_replace() [function.preg-replace]: Unknown modifier ‘+’ in /Applications/XAMPP/xamppfiles/htdocs/Site/index.php on line 210
Sorry to be a pain, but I found the solution.
replace:
with:
It works!
For some reason the tweets are showing up weird:
The #NewTwitter isn’t much new anymore… @kjkstudios
Don’t forget that once I reach 100 Subscribers I will GIVEAWAY an iTunes Gift Card. RT? http://bit.ly/cAg6Zd
looks like the (‘) is replaced with some random text?
please help me, i need this done really soon.
Hmm. Not too sure what is going on there. Do you have this live?
i just need to copy the codes into any part of the html file?
It needs to be a PHP file in order to work. But you can place it anywhere within a PHP file.
Hi, My website doesnt allow this function I think allow_url_open must be disabled. Is there an alternative solution to loading the file? Thanks
You may need to use cURL instead.
Thanks so much for sharing! Code works great and I was able to take it a little further and make a nice shortcode for a theme I am developing.
Any Demo about it?
Hey C, -
Great script, its working beautifully on my blog. The only issue is that the @username link is being appended with a colon like this @username:
What’s happening is that when someone clicks on that link to go to twitter it gets a page not found. I am not good enough at php to decipher where that extra colon is coming from, esp. that regex.
Thanks for any assistance.
Hmm. That’s weird. Have you changed the “bavotasan: ” to your username?
This is awsome, works on the fly. Only have to change URL to RSS.. It even takes RE: Tweets… Very nice.
Thanks man..
I was wondering how to insert “timesince”-variable in there..
Hey, very nice post … and tip! thanks for sharing this. keep the gret job
thank you I got the php file to work but how do integrate the php file into my html file?
thanks, great site!