├── .gitignore ├── README.md ├── README_zh.md ├── composer.json └── src ├── Contracts └── RssContract.php └── Rss.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## moell/rss 2 | moell/rss is a package that follows the RSS 2.0 standard 3 | 4 | ### RSS specification 5 | [http://www.rssboard.org/rss-specification](http://www.rssboard.org/rss-specification) 6 | 7 | ### 中文README 8 | [README](README_zh.md) 9 | 10 | ### Requirement 11 | PHP >= 5.4.0 12 | 13 | ### Installation 14 | ```shell 15 | composer require "moell/rss:1.*" 16 | ``` 17 | ### Provides an interface 18 | ```php 19 | 20 | public function setEncode($encode); //默认UTF-8 21 | 22 | public function channel(array $channel); 23 | 24 | public function item(array $item); 25 | 26 | public function items(array $items); 27 | 28 | public function build(); 29 | 30 | public function fastBuild(array $channel, array $item); 31 | 32 | public function __toString(); 33 | ``` 34 | 35 | ### Usage 36 | ```php 37 | $rss = new \Moell\Rss\Rss(); 38 | 39 | $channel = [ 40 | 'title' => 'title', 41 | 'link' => 'http://moell.cn', 42 | 'description' => 'description', 43 | 'category' => [ 44 | 'value' => 'html', 45 | 'attr' => [ 46 | 'domain' => 'http://www.moell.cn' 47 | ] 48 | ] 49 | ]; 50 | 51 | $rss->channel($channel); 52 | 53 | $items = []; 54 | for($i = 0; $i < 2; $i++) { 55 | $item = [ 56 | 'title' => "title".$i, 57 | 'description' => 'description', 58 | 'source' => [ 59 | 'value' => 'moell.cn', 60 | 'attr' => [ 61 | 'url' => 'http://www.moell.cn' 62 | ] 63 | ] 64 | ]; 65 | $items[] = $item; 66 | $rss->item($item); 67 | } 68 | 69 | echo $rss; //Get xml 70 | 71 | //Other acquisition methods 72 | $rss->build()->asXML(); 73 | 74 | $rss->fastBuild($channel, $items)->asXML(); 75 | 76 | $rss->channel($channel)->items($items)->build()->asXML(); 77 | ``` 78 | ### Generate results 79 | ```xml 80 | 81 | 84 | 85 | title 86 | http://moell.cn 87 | description 88 | html 89 | 90 | title0 91 | description 92 | moell.cn 93 | 94 | 95 | title1 96 | description 97 | moell.cn 98 | 99 | 100 | 101 | ``` 102 | 103 | ### License 104 | MIT 105 | -------------------------------------------------------------------------------- /README_zh.md: -------------------------------------------------------------------------------- 1 | ## moell/rss 2 | moell/rss是遵循RSS2.0标准的包 3 | 4 | ### RSS规范 5 | [http://www.rssboard.org/rss-specification](http://www.rssboard.org/rss-specification) 6 | 7 | 8 | ### 要求 9 | PHP >= 5.4.0 10 | 11 | ### 安装 12 | ```shell 13 | composer require "moell/rss:1.*" 14 | ``` 15 | ### 提供接口 16 | ```php 17 | //设置字符集 18 | public function setEncode($encode); //默认UTF-8 19 | 20 | public function channel(array $channel); 21 | 22 | public function item(array $item); 23 | 24 | public function items(array $items); 25 | 26 | //构造xml 27 | public function build(); 28 | 29 | //快速构造 30 | public function fastBuild(array $channel, array $item); 31 | 32 | public function __toString(); 33 | ``` 34 | 35 | ### 用法 36 | ```php 37 | $rss = new \Moell\Rss\Rss(); 38 | 39 | $channel = [ 40 | 'title' => 'title', 41 | 'link' => 'http://moell.cn', 42 | 'description' => 'description', 43 | 'category' => [ 44 | 'value' => 'html', 45 | 'attr' => [ 46 | 'domain' => 'http://www.moell.cn' 47 | ] 48 | ] 49 | ]; 50 | 51 | $rss->channel($channel); 52 | 53 | $items = []; 54 | for($i = 0; $i < 2; $i++) { 55 | $item = [ 56 | 'title' => "title".$i, 57 | 'description' => 'description', 58 | 'source' => [ 59 | 'value' => 'moell.cn', 60 | 'attr' => [ 61 | 'url' => 'http://www.moell.cn' 62 | ] 63 | ] 64 | ]; 65 | $items[] = $item; 66 | $rss->item($item); 67 | } 68 | 69 | echo $rss; //获取xml 70 | 71 | //其他获取方式 72 | $rss->build()->asXML(); 73 | 74 | $rss->fastBuild($channel, $items)->asXML(); 75 | 76 | $rss->channel($channel)->items($items)->build()->asXML(); 77 | ``` 78 | ### 生成结果 79 | ```xml 80 | 81 | 84 | 85 | title 86 | http://moell.cn 87 | description 88 | html 89 | 90 | title0 91 | description 92 | moell.cn 93 | 94 | 95 | title1 96 | description 97 | moell.cn 98 | 99 | 100 | 101 | ``` 102 | 103 | ### License 104 | MIT 105 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "moell/rss", 3 | "description": "moell/rss is a package that follows the RSS 2.0 standard", 4 | "keywords": ["rss"], 5 | "type": "library", 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Moell", 10 | "email": "moell91@foxmail.com" 11 | } 12 | ], 13 | "minimum-stability": "dev", 14 | "require": { 15 | "php" : ">=5.4.0" 16 | }, 17 | "autoload" : { 18 | "psr-4": { 19 | "Moell\\Rss\\": "src/" 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/Contracts/RssContract.php: -------------------------------------------------------------------------------- 1 | 5 | */ 6 | 7 | namespace Moell\Rss\Contracts; 8 | 9 | interface RssContract 10 | { 11 | public function setEncode($encode); 12 | 13 | public function channel(array $channel); 14 | 15 | public function item(array $item); 16 | 17 | public function items(array $items); 18 | 19 | public function build(); 20 | 21 | public function fastBuild(array $channel, array $item); 22 | 23 | public function __toString(); 24 | } -------------------------------------------------------------------------------- /src/Rss.php: -------------------------------------------------------------------------------- 1 | 5 | */ 6 | 7 | namespace Moell\Rss; 8 | 9 | use Moell\Rss\Contracts\RssContract; 10 | 11 | class Rss implements RssContract 12 | { 13 | protected $encode = 'UTF-8'; 14 | 15 | protected $channel; 16 | 17 | protected $items; 18 | 19 | public function setEncode($encode) 20 | { 21 | $this->encode = $encode; 22 | return $this; 23 | } 24 | 25 | /** 26 | * channel 27 | * 28 | * @param array $channel 29 | * 必选参数 30 | * - title 频道名称 31 | * - link 与频道关联的Web站点或者站点区域的Url 32 | * - description 频道描述 33 | * 34 | * 可选参数 35 | * - language 36 | * - compyright 37 | * - managingEditor 责任编辑的Email地址 38 | * - webMaster 频道相关网站管理员的Email地址 39 | * - pushDate 发布日期 40 | * - lastBuildDate 最后修改日期 41 | * - category 42 | * - ganerator 43 | * - docs 44 | * - cloud 45 | * - ttl 46 | * - images 47 | * - rating 48 | * - textInput 49 | * - skipHours 50 | * - skipDays 51 | */ 52 | public function channel(array $channel) 53 | { 54 | if (!isset($channel['title']) || !isset($channel['link']) || !isset($channel['description'])) { 55 | throw new \Exception('Required parameters:title,link,description'); 56 | } 57 | 58 | $this->channel = $channel; 59 | 60 | return $this; 61 | } 62 | 63 | /** 64 | * item 65 | * 66 | * @param array $item 67 | * 68 | * - title 69 | * - link 70 | * - descrition 71 | * - author 72 | * - category 73 | * - comments 74 | * - enclosure 75 | * - guid 76 | * - pushDate 77 | * - source 78 | */ 79 | public function item(array $item) 80 | { 81 | $this->items[] = $item; 82 | 83 | return $this; 84 | } 85 | 86 | /** 87 | * @param array $items 88 | */ 89 | public function items(array $items) 90 | { 91 | $this->items = $items; 92 | 93 | return $this; 94 | } 95 | 96 | 97 | /** 98 | * @return \SimpleXMLElement 99 | * @throws \Exception 100 | */ 101 | public function build() 102 | { 103 | if (empty($this->items) || empty($this->channel)) { 104 | throw new \Exception('Please set the required data'); 105 | } 106 | 107 | $xml = new \SimpleXMLElement('encode.'"?>'); 108 | 109 | $this->addItem($this->addChannel($xml)); 110 | 111 | return $xml; 112 | } 113 | 114 | /** 115 | * @param $xml 116 | */ 117 | private function addItem($xml) 118 | { 119 | foreach ($this->items as $it) { 120 | $item = $xml->addChild('item'); 121 | foreach ($it as $key => $value) { 122 | $this->addNode($key, $value, $item); 123 | } 124 | } 125 | } 126 | 127 | /** 128 | * add channel 129 | * 130 | * @param $xml 131 | */ 132 | private function addChannel($xml) 133 | { 134 | $node = $xml->addChild('channel'); 135 | 136 | foreach ($this->channel as $key => $value) { 137 | $this->addNode($key, $value, $node); 138 | } 139 | 140 | return $node; 141 | } 142 | 143 | /** 144 | * @param $key 145 | * @param $value 146 | * @param $xml 147 | */ 148 | private function addNode($key, $value, $xml) 149 | { 150 | if (is_array($value)) { 151 | if ($value['value'] != "") { 152 | $node = $xml->addChild($key, $value['value']); 153 | 154 | if (is_array($value['attr']) && !empty($value['attr'])) { 155 | foreach ($value['attr'] as $attrKey =>$attr) { 156 | $node->addAttribute($attrKey, $attr); 157 | } 158 | } 159 | } 160 | } else { 161 | $xml->addChild($key, $value); 162 | } 163 | } 164 | 165 | 166 | /** 167 | * @param array $channel 168 | * @param array $items 169 | */ 170 | public function fastBuild(array $channel, array $items) 171 | { 172 | $this->channel($channel); 173 | $this->items($items); 174 | return $this->build(); 175 | } 176 | 177 | public function __toString() 178 | { 179 | return $this->build()->asXml(); 180 | } 181 | 182 | } --------------------------------------------------------------------------------