winkers/yii2-rss

使用数组或数据库查询创建 RSS

安装次数: 9

依赖者: 0

建议者: 0

安全性: 0

星星: 0

Forks: 0

类型:yii2-extension

dev-master 2017-10-01 21:50 UTC

This package is not auto-updated.

Last update: 2024-09-22 15:01:02 UTC


README

使用数组或数据库查询创建 RSS

安装

安装此扩展的首选方法是使用 composer

运行以下命令之一

php composer.phar require --prefer-dist winkers/yii2-rss "dev-master"

或者在您的 composer.json 文件的 require 部分添加

"winkers/yii2-rss": "dev-master"

使用

一旦扩展安装完成,只需在您的代码中使用它即可

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

        $channel = [];
        $channel['title'] = 'My Title';
        $channel['link'] = 'http://example.com';
        $channel['description'] = 'The description will be come here.';
        $channel['language'] = 'en-us';

        /**
         * Get data by query for RSS uses
         */
        $data = (new \yii\db\Query())->select(['title', 'description'])->from('table')->all();

        /**
         * Get data by array for RSS uses
         */
        $data = [
            [
                'title' => 'My Title',
                'description' => 'The description will be come here.',
            ],
        ];

        $channel['items'] = [];

        foreach ($data as $k => $v) {
            $channel['items'][] = [
                'title' => $v['title'],
                'description' => substr($v['content'], 0, 500),
            ];
        }
        header('Content-Type: text/xml');
        echo $rss->createFeed($channel);

请注意,必须使用一种 queryarray 方法。