我是php的新手,想帮助生成rss feed。我有一个名为" test123"的数据库有字段ID,名称,地址,名称和文本。这是我的rss feed`代码
<?php
function connect() {
return new PDO('mysql:host=localhost;dbname=test123', 'root', '', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
}
$pdo = connect();
// posts *******************************
$sql = 'SELECT * FROM form ORDER BY id DESC';
$query = $pdo->prepare($sql);
$query->execute();
$rs_post = $query->fetchAll();
// The XML structure
$data = '<?xml version="1.0" encoding="UTF-8" ?>';
$data .= '<rss version="2.0">';
$data .= '<channel>';
$data .= '<title>PACRA Rss Feed</title>';
$data .= '<link>http://www.pacra.com</link>';
$data .= '<description>Pacra Pakistan</description>';
foreach ($rs_post as $row) {
$data .= '<item>';
$data .= '<title>'.$row['Name'].'</title>';
$data .= '<link>'.$row['Address'].'</link>';
$data .= '<description>'.$row['Text'].'</description>';
$data .= '</item>';
}
$data .= '</channel>';
$data .= '</rss> ';
header('Content-Type: application/xml');
echo $data;
?>`
问题是,当我运行该代码时,它显示错误消息&#34;打开和结束标记不匹配&#34; 这是错误的图像 Image Link
答案 0 :(得分:1)
它可能是标题,我在PHP生成的RSS源中使用以下内容:header('Content-type: text/xml; charset=UTF-8');
使用<title>
转义<description>
和CDATA
。
您是否使用RSS验证程序验证了Feed? https://validator.w3.org/feed/
我的结构也有点不同:
<?xml version="1.0" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<atom:link href="http://www.website.com" rel="self" type="application/rss+xml" />
<title>RSS Title</title>
<link>http://www.website.com</link>
<description>Description</description>
<language>en</language>
<managingEditor>Name</managingEditor>
<webMaster>mail@mail.com</webMaster>
<item>
<pubDate>Wed, 02 Oct 2015 15:00:00 +0200</pubDate>
<title><![CDATA[Here the title]]></title>
<link>http://www.website.com/page</link>
<guid>http://www.website.com/page</guid>
<description><![CDATA[Here the content]]></description>
</item>
<item>
<pubDate>Wed, 02 Oct 2015 15:00:00 +0200</pubDate>
<title><![CDATA[Here the title2]]></title>
<link>http://www.website.com/page2</link>
<guid>http://www.website.com/page2</guid>
<description><![CDATA[Here the content2]]></description>
</item>
</channel>
</rss>