【问题标题】:RSS feed created with PHP only shows the title in the feed reader使用 PHP 创建的 RSS 提要仅在提要阅读器中显示标题
【发布时间】:2011-05-28 16:29:44
【问题描述】:

我正在使用以下 PHP 代码为 RSS 提要生成 XML,但它似乎无法正常工作。提要阅读器中没有显示简短描述,我看到的只是文章的标题。此外,所有文章都说它们是同时发表的。这是我第一次尝试设置 RSS 提要,所以我确信我犯了几个愚蠢的错误。

$result = mysql_query("SELECT * FROM blog ORDER BY id DESC LIMIT 10");

$date = date(DATE_RFC822);

header('Content-type: text/xml');

echo ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
echo ("<rss version=\"2.0\">\n");
echo ("<channel>\n");
echo ("<lastBuildDate>$date</lastBuildDate>\n");
echo ("<pubDate>$date</pubDate>\n");
echo ("<title>my website name</title>\n");
echo ("<description><![CDATA[the description]]></description>\n");
echo ("<link>http://my-domain.com</link>\n");
echo ("<language>en</language>\n");

$ch=100;
while ($a = mysql_fetch_array($result)) {
    $headline = htmlentities(stripslashes($a['subject']));
    $posturl = $a[perm_link];
    $content = $a['post'];
    $date = date(DATE_RFC822, $a['posted']);

    echo ("<item>\n");
    echo ("<title>$headline</title>\n");
    echo ("<link>$posturl</link>\n");
    echo ("<description><![CDATA[$content]]></description>\n");
    echo ("<guid isPermaLink=\"true\">$posturl</guid>\n");
    echo ("<pubDate>$date2</pubDate>\n");
    echo ("</item>\n");
}

echo ("</channel>\n");
echo ("</rss>\n");

【问题讨论】:

  • 请为此使用适当的 xml 解析器 ...例如 simplexml

标签: php xml rss


【解决方案1】:
  1. 您确定 $a['post'] 包含帖子吗?

  2. $a[perm_link] 中的数组索引缺少引号;

  3. 您存储日期的变量称为 $date,而您在提要中放入 $date2;

  4. (不影响功能,但是)为什么在循环之前声明 $ch var?

【讨论】:

  • 1.是的,100% 确定。我可以在 XML 文件中看到它。 3. 谢谢,不知道我是怎么错过的。这解决了日期问题,另一个问题现在似乎也得到了解决。
猜你喜欢
  • 1970-01-01
  • 2023-04-06
  • 2013-02-15
  • 1970-01-01
  • 1970-01-01
  • 2012-07-06
  • 1970-01-01
  • 2014-05-12
  • 2012-02-14
相关资源
最近更新 更多