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. Ben G on June 24, 2010 at 5:14 am

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

  2. Ben G on August 9, 2010 at 5:05 am

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

  3. Andrea on October 28, 2010 at 11:39 am

    I had some troubles getting twitter to work with my website but this gave me some ideas as to how to fix it. Thanks.

  4. James Toone on November 2, 2010 at 11:19 am

    Is there any way to remove the name of my twitter account before every post?

    Example:

    jamestoone: What a nice day

    • c.bavota on November 2, 2010 at 2:25 pm

      You can add another line like this at the end of the last foreach statement:

      $str = str_replace("jamestoone: ", "", $str);
  5. Fashionista on November 7, 2010 at 6:30 am

    Thanx a lot for that php code. It works great. Solved all my tweet-button problems :)

  6. Dan on November 11, 2010 at 8:36 am

    Deprecated: Function ereg_replace() is deprecated in /Applications/XAMPP/xamppfiles/htdocs/Site/index.php on line 210

    is there an alternative?

    • Dan on November 11, 2010 at 8:44 am

      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

    • Dan on November 11, 2010 at 9:10 am

      Sorry to be a pain, but I found the solution.
      replace:

      $str = ereg_replace("[[:alpha:]]+://[^[:space:]]+[[:alnum:]/]","<a href="\">\</a>", $title);

      with:

      $str = preg_replace("/([\w]+:\/\/[\w-?&amp;;#~=\.\/\@]+[\w\/])/i","<a href="$1">$1</A>",$title);

      It works!

  7. Dan on November 11, 2010 at 9:57 am

    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.

    • c.bavota on November 11, 2010 at 12:17 pm

      Hmm. Not too sure what is going on there. Do you have this live?

  8. video on November 29, 2010 at 2:10 pm

    i just need to copy the codes into any part of the html file?

    • c.bavota on November 29, 2010 at 2:28 pm

      It needs to be a PHP file in order to work. But you can place it anywhere within a PHP file.

  9. c-note on December 7, 2010 at 8:44 am

    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

    • c.bavota on December 7, 2010 at 11:51 am

      You may need to use cURL instead.

  10. dave on December 9, 2010 at 1:00 am

    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.

  11. WP Tricks on December 26, 2010 at 6:44 am

    Any Demo about it?

  12. Colin on February 24, 2011 at 6:27 pm

    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.

    • c.bavota on February 25, 2011 at 9:50 am

      Hmm. That’s weird. Have you changed the “bavotasan: ” to your username?

  13. Daniel on March 8, 2011 at 7:40 am

    This is awsome, works on the fly. Only have to change URL to RSS.. It even takes RE: Tweets… Very nice.

    Thanks man..

  14. Harry on March 21, 2011 at 5:00 pm

    I was wondering how to insert “timesince”-variable in there..

  15. seoconsultant on March 28, 2011 at 5:42 pm

    Hey, very nice post … and tip! thanks for sharing this. keep the gret job

  16. naynay on March 30, 2011 at 9:47 pm

    thank you I got the php file to work but how do integrate the php file into my html file?

    thanks, great site!