Nov
23
2009

A Better Twitter Feed for Your Web Site

by   |  Posted in Tutorials  |  61 comments

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.

About the author:

A freelance web developer living in Montreal who spends most of his time writing for this site and building Premium themes for WordPress. You can find him on Twitter @bavotasan.

Site5 Affiliate Link
Share the love...

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

Short URL: http://bit.ly/bbbwRH

Discussion 61 Comments

  1. Kristine Shue on December 6, 2009 at 12:44 pm

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

  2. Caramoan on December 25, 2009 at 1:19 am

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

  3. acai on January 14, 2010 at 2:02 pm

    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.

  4. Robert on January 23, 2010 at 9:18 am

    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?

    • c.bavota on January 25, 2010 at 1:48 pm

      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.

  5. Robert on January 25, 2010 at 3:48 pm

    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.

    • c.bavota on January 27, 2010 at 1:19 am

      I just emailed you a solution.

  6. Andrea on January 27, 2010 at 4:59 pm

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

  7. wong on February 12, 2010 at 1:02 am

    I thought getting from Twitter feed will also slow down the loading as usual?

    • c.bavota on February 12, 2010 at 11:42 am

      Since you are relying on another site it can slow down loading your Web page.

  8. Allen on February 19, 2010 at 6:43 am

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

    • c.bavota on February 22, 2010 at 1:50 pm

      The above code should be placed wherever you want the Twitter feed to appear. It is as easy as that.

  9. Nachrichten on February 25, 2010 at 10:03 am

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

  10. Erika on March 5, 2010 at 2:38 pm

    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,

    • c.bavota on March 6, 2010 at 11:00 am

      You must test PHP on a server or in a test environment using something like MAMP.

  11. Alfredo on March 9, 2010 at 1:00 am

    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 ?

    • c.bavota on March 9, 2010 at 10:36 am

      That is usually caused because you are missing a closing bracket or something similar. Double check all the code above that.

  12. adrian on March 10, 2010 at 10:43 pm

    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?

    • c.bavota on March 11, 2010 at 10:31 am

      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.

  13. adrian on March 11, 2010 at 10:17 pm

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

    • c.bavota on March 12, 2010 at 10:15 am

      You have better eyes than I have. Thanks for the fix.

  14. echusa on March 14, 2010 at 3:26 am

    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

    • c.bavota on March 15, 2010 at 3:29 pm

      Might be the comma after:

      $node->getElementsByTagName('title')->item(0)->nodeValue

      that is causing the issue.

  15. sts on March 19, 2010 at 12:21 pm

    Works great, thanks for coding

  16. Sergey on March 27, 2010 at 3:58 pm

    Is twitter much better than blogging? I do not think. The twitter is good for the announcement of articles and news! And for thanks plugin!

  17. Avi D on March 28, 2010 at 10:03 am

    Would this work if I only wanted to have a twitter feed that’s hashtag based and not personal RSS feed based?

  18. Mike on March 31, 2010 at 7:58 pm

    Couple questions:

    As far as customizing this, is it possible to do anything with the username, right now it is username: blah blah blah…

    Is it possible to do Username and then a line break and then the tweet? and style the username with CSS? Also wondering if there is a way to limit the characters displayed in the tweet and then link to the full tweet?

    Is this possible?

  19. Wikus du Plessis on April 16, 2010 at 5:59 am

    Hi there,

    Thank you for this. It was exactly what I was looking for.

    There seems to be one issue however, for some reason the | in between certain tweets also gets linked with the class “grey” to a search for |.

    Any idea why this is happening?

  20. Wikus du Plessis on April 16, 2010 at 6:01 am

    Sorry, that look like a capital I. The character I’m referring to is the one next to backspace on the keyboard. Not sure what it’s called.

    • Wikus du Plessis on April 16, 2010 at 11:39 am

      Hello,

      Sorry if I’m being a nuisance but I also seem to be receiving the following error message at time (doesn’t happen everytime):

      Warning: DOMDocument::load() [domdocument.load]: Opening and ending tag mismatch: P line 6 and H4 in http://twitter.com/statuses/user_timeline/39714585.rss, line: 8 in /home/sawebstu/public_html/php/tweets.php on line 5

  21. oksi on April 23, 2010 at 8:09 am

    Thanks, it’s works.

  22. JLSull on April 29, 2010 at 3:52 pm

    Hi, I’m receiving this error when I try this:

    Parse error: syntax error, unexpected T_OBJECT_OPERATOR, expecting ‘)’ in /usr/local/4admin/apache/vhosts/crystalrealm.net/httpdocs/twittertest.php on line 13

    • c.bavota on April 30, 2010 at 9:13 am

      In your file, which line is line 13?

  23. cipher on May 5, 2010 at 11:50 am

    I have similar problem as above,

    parse error, unexpected T_OBJECT_OPERATOR, expecting ‘)’ on line 32

    line : 32

     foreach ($doc-&gt;getElementsByTagName('item') as $node) {

    I had 3 such errors before and went away by eliminating space or such things.

    Let me know whats wrong

    • cipher on May 5, 2010 at 12:42 pm

      Sorry the line is same…

       $itemRSS = array ('title' =&gt; $node-&gt;getElementsByTagName('title')-&gt;item(0)-&gt;nodeValue);
  24. Brandon on May 19, 2010 at 3:23 am

    Hi,

    Is there anyway to output a container around the username meaning

    John:

    blah blah blah…

    ?

    Thanks,
    B

  25. icymar on June 1, 2010 at 10:16 pm

    Parse error: parse error, expecting `’)” in c:\easyphp1-8\www\test\facebook-twiiter\json\index.php on line 8

    $itemRSS = array(‘title’ => $node->getElementsByTagName(‘title’)->item(0)->nodeValue);

    • c.bavota on June 2, 2010 at 10:36 am

      I just copied and pasted the code above into a new file and uploaded it to one of my servers and it worked perfectly. The issue might be your test server environment.