October 11th, 2008

Add Your Twitter Status To Your Blog

For the longest time I have been wanting to add my Twitter status to my blog in place of my quote right under my blogs name in the header.  (see above)  Today I sat down and figured out what I needed to do to accomplish this and to my surprise it only took all of 10 minutes to complete.

What you need in order to achieve the same for your own blog is:

  1. Managed Fusion Url Rewriter and Reverse Proxy
    This is used to get around cross site scripting blocks that modern browsers impose on JavaScript.  It is used to create a proxy from a local URL to an external URL by fooling the browser in to thinking it is requesting the feed locally.
  2. jQuery
    This is my favorite JavaScript framework, you can use your own, but my examples will be in jQuery.

The first thing you need to do is add a defined spot in your blog where you want the jQuery to write your twitter text status to. I created the following tag in my blog that has an ID of “twitter-status”, the inside of this tag will be replaced with whatever status is downloaded from twitter.

<span><a href="http://www.twitter.com/nberardi">twitter status:</a>&nbsp;<q cite="http://www.twitter.com/nberardi" id="twitter-status">status loading...</q></span>

The second thing you need to do is add a RewriteRule to the rewriter rules for a reverse proxy to the Twitter API. The rewriter rule that I used is:

# get twitter status
RewriteRule ^/twitter-status\.(.*) http://twitter.com/users/show/nberardi.$1 [NC,P]

So if you go to http://www.coderjournal.com/twitter-status.xml, you will get the direct feed that comes from http://twitter.com/users/show/nberardi.xml.

The third and last thing you need to do is add the jQuery JavaScript to the bottom of the page right above the </html>. What this script does is make a GET request to the URL that I defined above and downloads the content as JSON, which creates an object so that it can be used by JavaScript.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script type="text/javascript">
    $.getJSON(
        "/twitter-status.json",
        { },
        // on successful call back
        function (data, textStatus) {
            $("#twitter-status").text(data.status.text);
        }
    );
</script>

So it was really that easy. And it can be done for any feed that you want to pull from anywhere on the internet, it just happened to be that in this case I wanted to grab a feed from twitter.

Tags: , ,

Social: kick it on DotNetKicks.com

This entry was posted on Saturday, October 11th, 2008 at 3:22 pm and is filed under How To, JavaScript. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

7 Responses to “Add Your Twitter Status To Your Blog”

  1. DotNetKicks.com Says:

    Add Your Twitter Status To Your Blog…

    You’ve been kicked (a good thing) – Trackback from DotNetKicks.com…

  2. Dew Drop - October 12, 2008 | Alvin Ashcraft's Morning Dew Says:

    [...] Add Your Twitter Status to Your Blog (Nick Berardi) [...]

  3. Chris Says:

    My server requires that a file (ex. http://www.coderjournal.com/twitter-status.xml) actually exists before the rewriter proxy will work. Have you run into this?

  4. Nick Berardi Says:

    Yeah you need to enabled wildcard support in IIS 6.0. I put a short how to in the read me about how to turn this on.

  5. Chris Says:

    Thanks. I wrote an article about installing and configuring Managed Fusion’s URL Rewriter. I also posted my regular expressions used for additional URL Rewriting and blocking… All inspired by your post! Check it out.

    http://www.dscoduc.com/post/2008/10/24/URLRewriting-for-the-masses.aspx

    Thanks!

  6. Turschte Says:

    That’s cool. I like it. Thank you!

  7. Will Hancock Says:

    Hi..

    Same idea using jQuery to pull it in.. I used this…

    $(“#myStatus”).load(“http://twitter.com/{username} #timeline li.latest-status”);

    With the added bonus of the HTML it loads in still has all hyperlinks in. Shame all that is removed from the RSS Feeds it provides.

    However now need to work on using JQuery to run through making all the relative links absolute to Twitter.

Leave a Reply