生成 RSS 的伪代码以及 RSS 文件内容规范和校验

383 阅读4分钟

RSS 的生成伪代码以及 RSS 文件内容规范和校验

使用 PHP 生成 rss.xml 的核心伪代码

$indexUrl       = 首页地址;
$rssUrl         = Rss 地址;

//  日期要符合 RFC-822 规范
$updateAtFormat = date('D, d M Y H:i:s O', 首页文档最后更新的时间戳);

$arrRss[] = '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">';
$arrRss[] = '<channel>';
$arrRss[] = '<title>首页标题</title>';
$arrRss[] = sprintf('<link>%s</link>', $indexUrl);
$arrRss[] = sprintf('<atom:link href="%s" rel="self" type="application/rss+xml" />', $rssUrl);
$arrRss[] = '<description>网站描述</description>';
$arrRss[] = '<copyright>版权信息</copyright>';
$arrRss[] = '<language>语言。例如:zh-cn</language>';
$arrRss[] = sprintf('<lastBuildDate>%s</lastBuildDate>', $updateAtFormat);

$articlesData = 文章数据;

foreach ($articlesData as $val) {

  $articleUrl      = 此文章的 URL;
  $createdAtFormat = date('D, d M Y H:i:s O', 此文章的最后的更新时间戳);

  $arrRss[] = '<item>';
  $arrRss[] = sprintf('<title>%s</title>', $val->title);
  $arrRss[] = sprintf('<link>%s</link>', $articleUrl);
  $arrRss[] = sprintf('<description>%s</description>', $val->desc);
  $arrRss[] = sprintf('<pubDate>%s</pubDate>', $createdAtFormat);
  $arrRss[] = sprintf('<guid>%s</guid>', $articleUrl);
  $arrRss[] = '</item>';

}

$arrRss[] = '</channel>';
$arrRss[] = '</rss>';

$strRss = implode(PHP_EOL, $arrRss);

返回 $strRes 时记得设置 header 的 Content-Type 为text/xml'

网站增加 Rss 链接地址的示例

<a href="https://housanpai.com/rss.xml" rel="self" type="application/rss+xml">Rss</a>

本站 rss.xml 的部分内容,经过 w3 验证无误

<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>后三排</title>
        <link>https://housanpai.com</link>
        <atom:link href="https://housanpai.com/rss.xml" rel="self" type="application/rss+xml" />
        <description>后三排的个人记录</description>
        <copyright>Copyright housanpai.com</copyright>
        <language>zh-cn</language>
        <lastBuildDate>Sat, 26 Aug 2017 15:30:01 +0800</lastBuildDate>
        <item>
            <title>免费 HTTPS 证书部署 - Let's Encrypt</title>
            <link>https://housanpai.com/articles/1</link>
            <description>使用浏览器支持度比较好、可信度比较高的 Let's Encrypt 在 Centos7、Nginx 的服务器环境中,部署 https。客户端为 Let's Encrypt 推荐的 Certbot,本文包括 Certbot 客户端的下载、https 的生成、Nginx 的 https 443 80 的配置以及此对此证书的续期。</description>
            <pubDate>Fri, 18 Aug 2017 15:56:47 +0800</pubDate>
            <guid>https://housanpai.com/articles/1</guid>
        </item>
        <item>
            <title>我经历的众筹互助</title>
            <link>https://housanpai.com/articles/2</link>
            <description>个人来说,也一直对朋友圈的互助都有所捐助,自己也没想到有参与进来的一天。事情是堂哥家的双胞胎孩子早产,孩子都住进了保温箱,在住了一段时候后,我把轻松筹发给了哥哥,后续在转发过程当中的一些经历和感想...</description>
            <pubDate>Sat, 26 Aug 2017 14:15:15 +0800</pubDate>
            <guid>https://housanpai.com/articles/2</guid>
        </item>
        <item>
            <title>RESTful API 接口设计整理</title>
            <link>https://housanpai.com/articles/3</link>
            <description>规范统一的接口设计,有利于提高开发效率和编程的愉悦性。一直留意着相关的文章资料,经过一定的积累后,对这些资料进行了整理。在安全、URI 定义、查询参数、排序、过滤、版本、状态码、缓存、响应主体、错误码、Headers,等这些方面总结 RESTful API  的接口设计</description>
            <pubDate>Sat, 19 Aug 2017 22:08:34 +0800</pubDate>
            <guid>https://housanpai.com/articles/3</guid>
        </item>
        <item>
            <title>vimrc 配置</title>
            <link>https://housanpai.com/articles/4</link>
            <description>个人习惯上比较喜欢简单的,够用就好。日常开发大部分时间使用 vim,整理了下个人的 vimrc 配置把这个分享出来。</description>
            <pubDate>Mon, 21 Aug 2017 12:49:47 +0800</pubDate>
            <guid>https://housanpai.com/articles/4</guid>
        </item>
        <item>
            <title>Centos7 下源码编译 Golang 环境以及输出 hellow word</title>
            <link>https://housanpai.com/articles/5</link>
            <description>在 Centos7 系统中安装高版本的 Golang 环境的时候,尝试了各种方式,总是无法编译过去,后经过朋友介绍大牛指点,正确的使用源码编译的方式安装了 Golang,并且成功的输出了 hellow word</description>
            <pubDate>Fri, 25 Aug 2017 01:30:14 +0800</pubDate>
            <guid>https://housanpai.com/articles/5</guid>
            </item>
        <item>
            <title>VIM 程序猿神器 HHKB Pro BT 无刻键盘的 DIP SW 设置以及键盘快捷键</title>
            <link>https://housanpai.com/articles/6</link>
            <description>在亚马逊购买的 「VIM 程序猿神器 HHKB Pro BT 无刻键盘」,自己使用了一段时间,感觉不错,特此整理了一下 DIP SW 设置以及键盘快捷键的是用。</description>
            <pubDate>Sat, 26 Aug 2017 15:30:01 +0800</pubDate>
            <guid>https://housanpai.com/articles/6</guid>
        </item>
    </channel>
</rss>

其它

日期格式必须符合 RFC-822 规范,有效日期示例

  • Wed, 02 Oct 2002 08:00:00 EST
  • Wed, 02 Oct 2002 13:00:00 GMT
  • Wed, 02 Oct 2002 15:00:00 +0200

使用 w3 校验你生成的 Rss

点击打开 w3 校验地址 输入你的 Rss 地址,就会给出相应的结果。可以尝试本站的 Rss 地址 housanpai.com/rss.xml

知识共享许可协议
本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可。 随喜