utlime/php-rss-writer

优化的 RSS 编写器

1.1.0 2017-07-05 17:07 UTC

This package is auto-updated.

Last update: 2024-09-11 16:26:21 UTC


README

优化的 RSS 编写器,基于 Iterator 接口。这允许实现懒加载模式。

规范

https://validator.w3.org/feed/docs/rss2.html

安装

您可以直接通过 Composer 安装

$ composer require "utlime/php-rss-writer":"^1.0"

示例

examples/example.php

<?php

require "../vendor/autoload.php";

$item_iterator = function ($prefix) {
    for ($i = 0; $i < 3; $i++) {
        $item = new \Utlime\RSS\Item();

        $item->setTitle($prefix . ' title' . $i);

        yield $item;
    }
};

$channel_iterator = function () use ($item_iterator) {
    for ($i = 0; $i < 2; $i++) {
        $channel = new \Utlime\RSS\Channel('title' . $i, 'link' . $i, 'description' . $i);

        $channel->setItems($item_iterator('channel' . $i));

        yield $channel;
    }
};

$output = function ($string) {
    echo $string . "\r\n";
};

$rss = new \Utlime\RSS\RSS();
$rss->setChannels($channel_iterator());

$writer = new Utlime\RSS\Writer($output);
$writer->write($rss);

Feed 格式

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
    <channel>
        <title></title>
        <link></link>
        <description></description>
        <language></language>
        <copyright></copyright>
        <pubDate></pubDate>
        <lastBuildDate></lastBuildDate>
        <category></category>
        <ttl></ttl>
        <item>
            <title></title>
            <link></link>
            <description></description>
            <category></category>
            <guid></guid>
            <pubDate></pubDate>
            <enclosure></enclosure>
        </item>
    </channel>
</rss>

如何测试

$ composer install --dev
$ vendor/bin/phpunit

许可证

MIT 许可证