A Better Twitter Feed for Your Web Site

Posted on November 23, 2009  |  Category: Tutorials

twitternew A Better Twitter Feed for Your Web SiteI 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.


Tags: , , , , , , , , , , , , , , , , , , , , , ,

Short URL: http://bavotasan.com/?p=1545

24 Responses to “A Better Twitter Feed for Your Web Site”

  1. i just love to Twitter everyday with my friends. Twitter is much better than blogging in my opinion and it is very addictive too.

    #6633
  2. Twitter is some ways is much better than blogging. I love to Twitter my everyday activities on my friends and relatives.
    ***

    #6903
  3. 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.

    #7110
  4. 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?

    #7246
    • 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.

      #7280
  5. 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.

    #7283
  6. Thanks, I’m trying to get this working now.

    #7339
  7. I thought getting from Twitter feed will also slow down the loading as usual?

    #7656
  8. Allen

    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..

    #7780
  9. Amazing, the script is perfect. I think Twitter is a must have for everyone :-)

    #7891
  10. 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,

    #8071
  11. 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 ?

    #8150
  12. adrian

    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?

    #8192
    • 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.

      #8211
  13. adrian

    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! :)

    #8218
  14. echusa

    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

    #8259
  15. sts

    Works great, thanks for coding

    #8382

Leave a Reply

To enter code in the comment box, please place it between <pre lang="php"> </pre> tags. You don't have to convert any characters to their hex/HTML code. Just add your code the way you would to any code editor.