Loading your tweets using the twitter API

When building this site I knew I wanted to load my twitter status, but I didn’t want to use a plugin. I thought that I would just use my twitter RSS link and parse the xml that way, but for some reason I would keep getting a 400 error on the xml file. So, i broke down and started to look into the twitter API

Twitter has made is really easy to pass the feed through a URL that you can just use to parse as JSON.  Below is the code I used to parse my twitter feed, and have written it where you can just copy and change the function variables for the user name, and number of tweets to show.  You’ll need to make sure you are using jQuery to make this work.

Demo

<script type=”text/javascript”>
function showTweets(block, username, number)
{
var html = ‘<ul>’;
//// twitter api url convention
var tweetFeed = ‘http://twitter.com/status/user_timeline/’ + username + ‘.json?count=’ + number + ‘&callback=?’;
$.getJSON(tweetFeed, function(d)
{
$.each(d, function(i,item)
{
html+=’<li>’+item.text+’</li>’;
})
html+=”</ul>”;
block.children().fadeOut(400,function() {
block.append(html);
})
})
}
$(function() {
//// All you need to change is the user name below – ‘levikoi’ – to your twitter user name
showTweets($(‘#tweets’), ‘levikoi’, 5)
});
</script>
<div id=”tweets”>
<!– this is where the tweets will load –>
</div>
Comments are closed.