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.






i just love to Twitter everyday with my friends. Twitter is much better than blogging in my opinion and it is very addictive too.
–
Twitter is some ways is much better than blogging. I love to Twitter my everyday activities on my friends and relatives.
***
I like this commentary. Thanks for allowing it star in its own spot. I get uneasy when I encounter angry righteous mobs hurrying to open purses of feathers and burbly the tar. So often we understand only a tiny part of the write up and rather we let angriness do the thinking for us.
Hi I have this same problem, but I don’t exactly know where in the template I should make the changes for it to work. Should I change widget_twitter.php?
widget_twitter.php is where the Twitter widget code is stored. If you change that file, then you need to use the widget in your sidebar. Should fix it all up for you.
Thank you very much c.bavota. I tried to change it myself but got a parse error in PHP, supposedly on this line:$doc = new DOMDocument();
I’m not really good at PHP, so I don’t know where I should insert this code inside widget_twitter.php.
Could you tell me which part I should change? I really appreciate your help.
I just emailed you a solution.
Thanks, I’m trying to get this working now.
I thought getting from Twitter feed will also slow down the loading as usual?
Since you are relying on another site it can slow down loading your Web page.
Hi…
can u please let me know where exactly should i place this code???would want to implement twitter integration with my website..so can u please guide me on the things that needs to be done..would appreciate ur help..
The above code should be placed wherever you want the Twitter feed to appear. It is as easy as that.
Amazing, the script is perfect. I think Twitter is a must have for everyone
Hi,
I have been trying to run the php page, but it does not work. Do i need to install any specific library before testing it?
Thanks,
You must test PHP on a server or in a test environment using something like MAMP.
Parse error: syntax error, unexpected T_VARIABLE in /hermes/bosweb/web069/b690/sl.”MYUSERNAME”/public_html/mbc/hanselygretel/index.php on line 34
line 34 is = $doc = new DOMDocument(); any idea what could be wrong ?
That is usually caused because you are missing a closing bracket or something similar. Double check all the code above that.
hi… i trying out this code.
sorry but from your earlier reply, i just need to copy the codes into any part of the html file? i was testing it out with a plain html file (called 1.html) and it does not seem to work. also tried to rename the file as 1.php but it does not work either.
do we need to change anything else besides the url?
It needs to be saved as a PHP file and you need to upload it to a server or into a test environment to see it work. It will not work if you just test in from your desktop.
hi again… messed around with the settings and finally got it to work.
as for the error from alfredo, i believe its on this line:
$feedURL = “http://twitter.com/statuses/user_timeline/68559295.rss” // change to your feed URL
there should be a ; after .rss”
i got the same error and after adding it, no more errors and its working! i’m definitely bookmarking your site! lots i can learn from!
You have better eyes than I have. Thanks for the fix.
Parse error: parse error, unexpected T_OBJECT_OPERATOR, expecting ‘)’ in (FILENAME) on line 8
$itemRSS = array ( ‘title’ => $node->getElementsByTagName(‘title’)->item(0)->nodeValue,);
What is wrong with the code
Might be the comma after:
that is causing the issue.
Works great, thanks for coding