open20/open2-amos-rss

该包的最新版本(1.0.1)没有可用的许可信息。

AMOS RSS 系统

1.0.1 2022-09-13 12:32 UTC

This package is auto-updated.

Last update: 2024-09-15 14:07:37 UTC


README

Amos 框架扩展,通过 zend-feed 库提供消费 RSS 和 Atom 联系人的功能。

安装

{
	"require": 
	{
  		"open20/amos-rss": "~1.0.0"
	}
}

配置

在配置文件中

/config/web.php

添加联系人组件

'components' => array(
        ...
        'rss' => array(
        	 	'class' => 'amos\rss\components\RssFeed',
        		),
		    )

简单使用

读取 RSS 联系人

$feed=Yii::$app->feed->reader()->import('http://exapmple.com/feed.rss');

这将获取 RSS 联系人,解析它并返回联系人对象。更多详细信息,您可以阅读官方 Zend-feed 扩展文档: http://framework.zend.com/manual/2.2/en/modules/zend.feed.reader.html

创建 RSS 联系人

在控制器中创建 RSS 动作

public function actionRss(){
		
     $feed=Yii::$app->feed->writer();
		
    $feed->setTitle(Yii::$app->params['title']);
		$feed->setLink('http://example.com');
		$feed->setFeedLink('http://example.com/rss', 'rss');
		$feed->setDescription(Yii::t('app','Recent headlines'));
		$feed->setGenerator('http://example.com/rss');
		$feed->setDateModified(time()); 
		/**
		* Add one or more entries. Note that entries must
		* be manually added once created.
		*/
		$posts=Post::find()->orderBy('id DESC')->limit(20)->all();
		foreach($posts as $post){
				$entry = $feed->createEntry();
				$entry->setTitle($post->title);
				$entry->setLink(Yii::$app->urlManager->createAbsoluteUrl('/post/view',['id'=>$post->id]));
				$entry->setDateModified(intval($post->created));
				$entry->setDateCreated(intval($post->created));
				$entry->setContent(
				   $post->content
				);
				$entry->setEnclosure(
					[
					 'uri'=>$post->image,
					 'type'=>'image/jpeg',
					 'length'=>filesize(Yii::getAlias('@webroot').$post->image)
					 ]
				);
				$feed->addEntry($entry);
		}
		/**
		* Render the resulting feed to Atom 1.0 and assign to $out.
		* You can substitute "atom" with "rss" to generate an RSS 2.0 feed.
		*/
		$out = $feed->export('rss');
		header('Content-type: text/xml');
		echo $out;
		die();
	}

将 rss 迁移添加到控制台模块(console/config/migrations-amos.php)

'@vendor/open20/amos-rss/src/migrations'
add rss module to backend modules (backend/config/modules-amos.php):

'rss' => [ 'class' => 'amos\rss\Module', 'modelsEnabled' => [ 'open20\amos\news\models\News', ], 'federationUrls' => [ 'http://domain.from.readrss.com' ] ],