php实现rss
RSS 生成基础
PHP 生成 RSS 需要遵循 XML 格式规范,核心是输出符合 RSS 2.0 标准的 XML 文档。需设置正确的 HTTP 头部声明内容类型为 application/rss+xml。
header('Content-Type: application/rss+xml; charset=utf-8');
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<rss version="2.0">';
构建频道信息
RSS 频道需包含标题、链接、描述等必需元素。示例结构包含基础字段和可选字段如语言、发布日期。
<channel>
<title>网站标题</title>
<link>https://example.com</link>
<description>网站描述内容</description>
<language>zh-cn</language>
<pubDate>' . date('r') . '</pubDate>
</channel>
添加文章条目
每个 <item> 节点代表一篇文章或内容条目,必须包含标题、链接和描述。支持扩展元素如分类、作者、发布时间。

<item>
<title>文章标题</title>
<link>https://example.com/article/123</link>
<description><![CDATA[文章摘要或内容]]></description>
<category>技术</category>
<author>author@example.com</author>
<guid isPermaLink="true">https://example.com/article/123</guid>
<pubDate>' . date('r', strtotime('2023-01-01')) . '</pubDate>
</item>
数据库动态生成
从数据库获取内容时,需遍历查询结果动态生成 <item> 节点。MySQL 示例展示如何获取文章数据并循环输出。
$pdo = new PDO('mysql:host=localhost;dbname=test', 'user', 'pass');
$stmt = $pdo->query('SELECT title, url, content FROM articles LIMIT 10');
while ($row = $stmt->fetch()) {
echo '<item>
<title>' . htmlspecialchars($row['title']) . '</title>
<link>' . htmlspecialchars($row['url']) . '</link>
<description><![CDATA[' . $row['content'] . ']]></description>
</item>';
}
完整示例代码
以下为整合数据库查询的完整 RSS 生成脚本,包含错误处理和 XML 结构闭合。确保所有输出内容经过 htmlspecialchars 转义。

<?php
header('Content-Type: application/rss+xml; charset=utf-8');
try {
$pdo = new PDO('mysql:host=localhost;dbname=test', 'user', 'pass');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<rss version="2.0"><channel>';
echo '<title>最新文章</title><link>https://example.com</link><description>网站最新更新</description>';
$stmt = $pdo->query('SELECT title, url, content, created_at FROM articles ORDER BY created_at DESC LIMIT 20');
while ($row = $stmt->fetch()) {
echo '<item>
<title>' . htmlspecialchars($row['title']) . '</title>
<link>' . htmlspecialchars($row['url']) . '</link>
<description><![CDATA[' . $row['content'] . ']]></description>
<pubDate>' . date('r', strtotime($row['created_at'])) . '</pubDate>
</item>';
}
echo '</channel></rss>';
} catch (PDOException $e) {
header('Content-Type: text/plain');
die('Database error: ' . $e->getMessage());
}
?>
使用 SimpleXML 生成
对于复杂需求可采用 SimpleXML 构建 RSS,该方法自动处理 XML 结构和转义。创建 XML 对象后逐步添加节点元素。
$rss = new SimpleXMLElement('<rss version="2.0"/>');
$channel = $rss->addChild('channel');
$channel->addChild('title', '网站标题');
$channel->addChild('link', 'https://example.com');
$item = $channel->addChild('item');
$item->addChild('title', '文章标题');
$item->addChild('description')->addCData('文章内容');
header('Content-Type: application/rss+xml');
echo $rss->asXML();
缓存优化策略
高频访问的 RSS 可采用缓存机制减少数据库压力。通过文件缓存或内存缓存存储生成的 XML,设置合理的过期时间。
$cacheFile = 'rss_cache.xml';
if (file_exists($cacheFile) && time() - filemtime($cacheFile) < 3600) {
readfile($cacheFile);
exit;
}
ob_start();
// 正常生成 RSS 内容
file_put_contents($cacheFile, ob_get_flush());
验证 RSS 有效性
生成后应通过 W3C Feed Validation Service 或本地工具验证 XML 结构是否正确。常见问题包括字符编码错误、未闭合标签或无效日期格式。






