CodeBlink.com Open Source Projects, Code, Rants, and other miscellany

Birdnapper – The PHP Twitter Feed Widget

Description:

Bridnapper 1.1 allows you to display the latest tweets of a user of your choosing. The user you choose must have their Tweets public, otherwise Birdnapper will not be able to display properly.

I developed this originally for use on my WordPress blog on www.codeblink.com sidebar, but I'm sure you will be able to find many other applications for it.

Source Files: Download 1.1 ZIP | Download 1.1 RAR

Installation:

1.) Copy and paste the following into a new file called birdnapper.php:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function birdnapping($strUsername,$intShowTweets) {
    $html_url="http://twitter.com/$strUsername";
    $handle = @fopen($html_url, "r");
    if ($handle) {
            while (!feof($handle)) {
                    $buffer = fgets($handle, 4096);
                    $content=$content.$buffer;
            }
            fclose($handle);
    }
    $content=trim($content);
   
    $start=strpos($content,"<div class=\"section\">");
    $end=strpos($content,"<div id=\"pagination\">");
    $tweets=substr($content,$start,$end-$start);
   
    $arrayTweets=explode("<li",$tweets);
   
    $tweets="";
   
    $i=1;
    while ($i<=$intShowTweets) {
        $start=strpos($arrayTweets[$i],">")+1;
        $strippedTweet=substr($arrayTweets[$i],$start,strlen($arrayTweets[$i])-$start);
        $tweets.="<li>".$strippedTweet;
        $i++;
    }
   
    // fix links to Twitter profiles. Twitter gives inline links
    $tweets=str_replace("href=\"/","href=\"http://twitter.com/",$tweets);
   
    return "<div id=\"birdnapping\">\n<ol>\n$tweets\n</ol>\n<div class=\"follow\"><a href=\"http://twitter.com/$strUsername\" target=\"_blank\">Follow $strUsername &raquo;</a></div>\n</div>";
}

3.) Create a folder called 'birdnapper' in your WP folder: /wp-content/plugins/ and save your birdnapper.php file there.

4.) Then copy and paste this code to display the Twitter Feed:

1
2
3
4
5
6
7
8
9
<ul>
 <li>
 <div class="sidebarbox">
 <h2 class="widgettitle">Twitter Feed</h2>
 <?php include($_SERVER['DOCUMENT_ROOT']."/wp-content/plugins/birdnapper/birdnapper.php"); ?>
 <?php echo birdnapping("TwitterUserName",5); ?>
 </div>
 </li>
</ul>

5.) Make sure to replace 'TwitterUserName' with the user name of the person you wish to display Tweets from.

6.) The Birdnapper Twitter feed can be styled anyway you wish with CSS. This is the major advantage it has over an RSS feed. Here is the CSS I currently use:

1
2
#birdnapping span.entry-meta{ display:block; font-style:italic; font-size:smaller; }
#birdnapping .follow { font-weight:bold; }

In Development for Version 1.1:

  • Option to allow Birdnapper to scrape from a Twitter User or search.twitter.com using a search term.
  • Custom Filtering
    • Swearing
    • Exclude specific Twitter authors

1.1 Update

Added a line of code to fix inline links to Twitter profile pages. Links will now be external to http://twitter.com. Discovered this flaw when I checked out my Google Webmaster report!

POLL: What do you think? (Birdnapper 1.1)

View Results

Loading ... Loading ...

Please leave a comment or suggestion below!

  • Share/Bookmark
  • That's awesome Kevin! I'll try it out and make an update as soon as I get the time. I've actually had a 1.2 update on the burner for some time now and just haven't had time to fully test and update the code. I'll try to implement your changes into the update.
  • I have updated this widget so that it displays in an easier to read manner. Also, It's now valid with XHTML 1.0 Strict doctypes, and can be easily set to display as a series of divs, unordered list, or ordered list. I will try and contact the originator of this code in order to have him add my changes here, but in the meantime you can find the new version at:

    http://www.kevinparkerson.com/birdnapperWidget.html

    Do anything you'd like with it, and post a link if you've made any worthwhile changes! Enjoy! : )
blog comments powered by Disqus