RSSgenerator.php

Třída pro generování RSS

php

<?php

class RSSgenerator extends XMLwriter {
    private $channels = array();

    function addChannel($seznam) {
        $this->channels[] = $seznam;
    }

    function __toString() {
        $this->openMemory();
        $this->setIndent(true);
        $this->startDocument('1.0', 'UTF-8');
        $this->startElement('rss');
        $this->writeAttribute('version','2.0');
        $this->writeAttribute('xmlns:content', "http://purl.org/rss/1.0/modules/content/");
        $this->writeAttribute('xmlns:wfw', "http://wellformedweb.org/CommentAPI/");
        $this->writeAttribute('xmlns:dc', "http://purl.org/dc/elements/1.1/");
        foreach ($this->channels as $channel) {
            $this->writeChannel($channel);
        }
        $this->endElement();
        $this->endDocument();
        return $this->flush();
    }

    function writeChannel($channel) {
        $this->startElement('channel');
        $this->writeElement('title', $channel['title']);
        $this->writeElement('link', $channel['link']);
        $this->writeElement('description', $channel['description']);
        $this->writeImage($channel);
        $this->writeItems($channel['items']);
        $this->endElement();
    }

    function writeImage($channel) {
        $this->startElement('image');
        $this->writeElement('title', $channel['title']);
        $this->writeElement('link', $channel['link']);
        $this->writeElement('url', $channel['logo']);
        $this->endElement();
    }

    function writeItems($items) {
        foreach ($items as $item) {
            $this->startElement('item');
            $this->writeElement('title', $item->title);
            $this->writeElement('link', $item->link);
            $this->writeElement('pubDate', $item->pubDate);
            $this->writeElement('description', $item->description);
            $this->writeElement('dc:creator', $item->creator);
            $this->endElement();
        }
    }

$rss = new RSSgenerator();
$db=new PDO('sqlite:rss.sqlite');
$result=$db->query("SELECT * FROM clanek");
$rss->addChannel(array(
    'items' => $result->fetchAll(PDO::FETCH_OBJ),
    'title' => 'devbook',
    'link' => 'http://www.devbook.cz/',
    'description' => 'devbook - Programátorská sociální síť a materiálová základna',
    'logo' => 'http://www.devbook.cz/logo.png',
));
header('Content-Type: text/xml');
echo $rss;

Neformátovaný

Přidáno: 14.8.2013
Expirace: Neuvedeno

Avatar
Autor: Kit
Aktivity (1)