I just added Twitter into my Feed

I've been trying to figure out how to integrate my Twitter posts into this site... I'm not exactly sure the best way just yet, but I'm going to play with some ideas. At first, since I'm using FeedBurner to handle my feed traffic, I thought I could just "splice" in the Twitter feed along with my normal one, but it turns out there's only a few sites that works with - you can just add some random feed URL. So then I started trying to think of how I wanted to write my own code...

I was thinking about using the API and adding a "Tumbleblog" like option to my new Admin interface that would let me write Twitter posts, or add a checkmark to my current posting form which would update Twitter *and* save it to the database as well. But my blog is sort of designed for lengthy posts, because that's what I normally write, so a bunch of updates would ruin the aesthetic. Besides, I kinda like using IM to update Twitter... it makes the mini-updates feel more natural.

Thus I decided to return to the splicing thing but to write it myself using SimplePie since I already had a template for it already sitting around. That library is insanely kick-ass and will do all the hard work of parsing the external feeds, merging them and ordering the items by date for me. I just then have to write it back out as a feed, which is no biggie.

Here's the code in PHP:


<?php

        require 'simplepie.inc';

        $feed = new SimplePie();
        $feed->set_feed_url(array(
                'http://www.russellbeattie.com/blog/my_hidden_feed',
                'http://twitter.com/statuses/user_timeline/4826261.atom'
        ));

        $feed->init();

        header('Content-type: application/atom+xml; charset=UTF-8', true);

        echo '<?xml version="1.0" encoding="utf-8"?' . '>' . "n";
?>

<feed xmlns="http://www.w3.org/2005/Atom">
        <title type="text"><?=$site['title']?></title>
        <subtitle type="text"><?=$site['description']?></subtitle>
        <updated><?=$feed->get_item()->get_date(DATE_ATOM) ?></updated>

        <author>
                <name><?=$site['owner']?></name>
                <email><?=$site['email']?></email>
        </author>
        <logo><?=$site['image']?></logo>

<?
                foreach($feed->get_items() as $item) {
?>
        <entry>
                <title type="html"><![CDATA[<?=$item->get_title()?>]]></title>
                <link rel="alternate" type="text/html" href="<?=$item->get_link(0)?>" />

                <id><?=$item->get_id()?></id>
                <updated><?=$item->get_date(DATE_ATOM)?></updated>
                <content type="html"><![CDATA[<?=$item->get_content()?>]]></content>

        </entry>
<?
                }
?>
</feed>

Obviously if you're going to use this for your site, you'd have to replace the URLs, and the $site[] array variables for your blog's specifics... but it's pretty generic, so it shouldn't be a big deal.

Hope you find the code useful, and we'll see how long it is before I repent and pull out the Twitter updates... :-)

-Russ

< Previous         Next >