Recently I was working with a client who wanted a blog for their site but they didn’t have the money (and I didn’t have the time) for a completely one-off solution. The initially proposed solution was to simply make the “Blog” link point to a Blogger (which was purchased by Google a while back, if you missed it) site for the client. I didn’t feel very comfortable with this idea because I didn’t think it would look very professional to link to a free blogging site, so I started out by using the template editor that Blogger provides to mimic the client’s site. While this was a better solution, it still didn’t seem professional enough. The site was still a Blogger site; the URL was Blogger’s and I couldn’t exactly match the look-and-feel of the client’s site.

This is when I remembered that Blogger (as many with many other sites) provides an XML-based RSS feed of each individual blog. Why not consume the RSS feed and redisplay the content on the client’s site?! This technique allowed me to utilize the blog publishing and management tools from Blogger while full integrating the blog into the client’s site. Now, instead of linking to an external site for the blog, I would just create a page that scraped the RSS feed, parsed the XML, iterated over the entries, and displayed the content.

Now for the meat. First, you need to create a Blogger account (if you have a Google/Gmail/G-anything account, that will work), then create a blog under that account and enter some dummy posts. Make sure that your blog has site feeds enabled; consult the Blogger help if you can’t figure this out. Once you have that setup, your blog’s feed can be found at http://someblog.blogspot.com/feeds/posts/default?alt=rss where “someblog” is the name of your blog in your URL. Also, you’ll notice that I’ve appended ?alt=rss to the link. This is mentioned in the Blogger documentation in reference to forcing the feed into RSS format rather than Atom.

The following PHP code example shows a function that parses an RSS feed into a 2D array for display within your site. Note: the simplexml_load_file() function is part of PHP5, so you’ll need to make sure that your server is running PHP5 if you want to use this code directly.

<?php
    function getBlogPosts($blogFeedURL)
    {
        $xml = simplexml_load_file($blogFeedURL);
        $posts = array();

        if($xml)
        {
            // get all posts
            foreach($xml->channel->item as $post)
            {
                $posts[] = array('title' => $post->title,
                                    'pubDate' => $post->pubDate,
                                    'description' => $post->description);
            } // end foreach loop
        } // end if test
        return $posts;
    } // end getBlogPosts function
?>

This next block of code utilizes this function and iterates over the 2D array to display the blogs. Also, I’ve incorporated the concept of paging where you can specify the number of posts to display at a given time and paging links are found at the top and bottom of each page.

<?php
    global $blogFeedURL; // defined globally, URL to RSS feed
    global $blogPostsPerPage; // defined globally, used for paging
    $posts = getBlogPosts($blogFeedURL);

    if(count($posts) < 1)
    {
        ?>
            <p>There are no posts currently available.</p>
        <?php
    } // end if test
    else
    {
        // get paging index from query string
        $firstPost = (array_key_exists('first', $_GET) &&
                                is_numeric($_GET['first'])) ?
                                $_GET['first'] : 0;

        // if first post index plus offset is more than total posts,
        // set last post to total number of posts
        $lastPost = (($firstPost + $blogPostsPerPage) > count($posts)) ?
                                count($posts) : ($firstPost + $blogPostsPerPage);

        // previous page link
        if($firstPost > 0)
        {
            $first = (($firstPost - $blogPostsPerPage) < 0) ?
                                0 : ($firstPost - $blogPostsPerPage);
            ?>
                <p><a href="<?php echo $_SERVER['PHP_SELF'] . '?first=' .
                                $first; ?>">Previous Page</a></p>
            <?php
        } // end if test

        // next page link
        if($lastPost < count($posts))
        {
            $first = (($firstPost + $blogPostsPerPage) > count($posts)) ?
                                count($posts) : ($firstPost + $blogPostsPerPage);
            ?>
                <p><a href="<?php echo $_SERVER['PHP_SELF'] . '?first=' .
                                $first; ?>">Next Page</a></p>
            <?php
        } // end if test

        // display only those for this page
        for($i = $firstPost; $i < $lastPost; $i++)
        {
            $title = $posts[$i]['title'];
            $pubDate = date('D, M j, Y g:i A', strtotime($posts[$i]['pubDate']));
            $description = $posts[$i]['description'];
        ?>
            <div>
                <p><?php echo $title; ?></p>
                <p><?php echo $pubDate; ?></p>
                <p><?php echo $description; ?></p>
            </div>
        <?php
        } // end foreach loop

        // previous page link
        if($firstPost > 0)
        {
            $first = (($firstPost - $blogPostsPerPage) < 0) ?
                                0 : ($firstPost - $blogPostsPerPage);
            ?>
                <p><a href="<?php echo $_SERVER['PHP_SELF'] . '?first=' .
                                $first; ?>">Previous Page</a></p>
            <?php
        } // end if test

        // next page link
        if($lastPost < count($posts))
        {
            $first = (($firstPost + $blogPostsPerPage) > count($posts)) ?
                                count($posts) : ($firstPost + $blogPostsPerPage);
            ?>
                <p><a href="<?php echo $_SERVER['PHP_SELF'] . '?first=' .
                                $first; ?>">Next Page</a></p>
            <?php
        } // end if test
    } // end else test
?>

As I said earlier, the great thing about this method is that someone else (Google and Blogger) did all the hard work for you. The downside is that you have to login to Blogger to post content, but I think that’s a small price to pay for the amount of work that is already done for you. You get the layout-free content and you can style it yourself and embed it in your own site as you see fit. Alternately, you could of course do this in Python, Java, .Net, or any language that you can make an HTTP call and then parse the XML.

As for the usual caveats, I’m sure there are ways to improve the security, add layers of error checking, use objects instead of a 2D array, so feel free to modify as you wish. This is where the “This software is presented as-is and without warranty…” bit goes.