moell/rss 好用的輪子,輕鬆實現網站RSS功能,遵循RSS2.0標準

moell/rss

moell/rss是遵循RSS2.0標準的PHP包,Laravel版本請查看moell/laravel-rssphp

源碼地址

moell/rss
moell/laravel-rsshtml

RSS規範

http://www.rssboard.org/rss-specificationlaravel

要求

PHP >= 5.4.0git

安裝

composer require "moell/rss:1.*"

提供接口

//設置字符集
public function setEncode($encode); //默認UTF-8

public function channel(array $channel);

public function item(array $item);

public function items(array $items);

//構造xml
public function build();

//快速構造
public function fastBuild(array $channel, array $item);

public function __toString();

用法

$rss = new \Moell\Rss\Rss();

$channel = [
    'title' => 'title',
    'link'  => 'http://moell.cn',
    'description' => 'description',
    'category' => [
        'value' => 'html',
        'attr' => [
            'domain' => 'http://www.moell.cn'
        ]
    ]
];

$rss->channel($channel);

$items = [];
for($i = 0; $i < 2; $i++) {
    $item = [
        'title' => "title".$i,
        'description' => 'description',
        'source' => [
            'value' => 'moell.cn',
            'attr' => [
                'url' => 'http://www.moell.cn'
            ]
        ]
    ];
    $items[] = $item;
    $rss->item($item);
}

echo $rss;    //獲取xml

//其餘獲取方式
$rss->build()->asXML();

$rss->fastBuild($channel, $items)->asXML();

$rss->channel($channel)->items($items)->build()->asXML();

生成結果

<?xml version="1.0" encoding="UTF-8"?>
<rss
    xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
    <channel>
        <title>title</title>
        <link>http://moell.cn</link>
        <description>description</description>
        <category domain="http://www.moell.cn">html</category>
        <item>
            <title>title0</title>
            <description>description</description>
            <source url="http://www.moell.cn">moell.cn</source>
        </item>
        <item>
            <title>title1</title>
            <description>description</description>
            <source url="http://www.moell.cn">moell.cn</source>
        </item>
    </channel>
</rss>

License

MITgithub

若是你喜歡本項目,歡迎star。GitHub
轉帖需說明文章出處:http://moell.cn/article/25shell

相關文章
相關標籤/搜索