Merging feeds using PHP's DOM

Someone Geoffrey Sneddon (one of the SimplePie devs) left me a comment complaining about my quick and dirty feed merger script in PHP. It hadn't considered it before, but my CData fields could break. I was thinking, "I should do this the right way..." and since I use the DOM a lot in my transcoding stuff, I'm familiar with the methods. I mean, it's not hard, it's just very, very verbose.

Attached below.

:-)

-Russ


<?php

        require 'simplepie.inc';

        $feed = new SimplePie();
        $feed->enable_cache(false);
        $feed->set_feed_url(array(
                'http://feeds.russellbeattie.com/russellbeattieweblog',
                'http://twitter.com/statuses/user_timeline/4826261.atom'
        ));

        $feed->init();

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

        $doc = new DOMDocument();
        $doc->formatOutput = true;

        $root = $doc->createElement('feed');
        $doc->appendChild($root);

        $root->setAttribute('xmlns', 'http://www.w3.org/2005/Atom');

        $element = $doc->createElement('title');
        $element->appendChild($doc->createTextNode($site['title']));
        $root->appendChild($element);

        $element = $doc->createElement('subtitle');
        $element->appendChild($doc->createTextNode($site['description']));
        $root->appendChild($element);

        $element = $doc->createElement('updated');
        $element->appendChild($doc->createTextNode($feed->get_item()->get_date(DATE_ATOM)));
        $root->appendChild($element);

        $element = $doc->createElement('author');

                $subelement=$doc->createElement('name');
                $subelement->appendChild($doc->createTextNode($site['owner']));
                $element->appendChild($subelement);

                $subelement=$doc->createElement('email');
                $subelement->appendChild($doc->createTextNode($site['email']));
                $element->appendChild($subelement);          

        $root->appendChild($element);

        $element = $doc->createElement('logo');
        $element->appendChild($doc->createTextNode($site['image']));
        $root->appendChild($element);

        foreach($feed->get_items() as $item) {

                $entry = $doc->createElement('entry');

                $element = $doc->createElement('title');
                $element->appendChild($doc->createCDATASection($item->get_title()));
                $entry->appendChild($element);

                $element = $doc->createElement('link');
                $element->setAttribute('rel','alternate');
                $element->setAttribute('type','text/html');
                $element->setAttribute('href',$item->get_link(0));
                $entry->appendChild($element);               

                $element = $doc->createElement('id');
                $element->appendChild($doc->createTextNode($item->get_id()));
                $entry->appendChild($element);

                $element = $doc->createElement('updated');
                $element->appendChild($doc->createTextNode($item->get_date(DATE_ATOM)));
                $entry->appendChild($element);

                $element = $doc->createElement('content');
                $element->appendChild($doc->createCDATASection($item->get_content()));
                $element->setAttribute('type','html');
                $entry->appendChild($element);

                $root->appendChild($entry);

        }

        echo $doc->saveXML();

?>

< Previous         Next >