如何使用 PHP 和 MySQL 创建 RSS

问题描述 投票:0回答:3

我尝试制作一个 RSS 提要来从我的 SQL 数据库加载数据。

当我运行它时,要么它会带我到互联网浏览器上的 RSS 阅读器并显示“没有内容”,要么会返回错误:

此页面包含以下错误:第 1 列第 2 行错误:文档末尾有额外内容 下面是第一个错误之前的页面呈现。

我正在使用 Dreamweaver 制作 RSS 并在 PHP 文件上创建它。

<?php require_once('Connections/mydatabase.php'); ?>
<?php 
header('Content-type: text/xml');
$mydatabase = mysqli_connect("http://127.0.0.1","root","au291826","db_cms");
$rss = '<?xml version="1.0" encoding="utf-8"?>';
$rss .= '<rss version="2.0">';
$rss .= '<channel>';
$rss .= '<title>My RSS</title>';
$rss .= '<link>http://localhost/cws_files/</link>';
$rss .= '<description>implementation of RSS with PHP </description>';
$rss .= '<language>ar-sa</language>';
$rss .= '<copyright> RSS </copyright>';
mysql_select_db($database_mydatabase, $mydatabase);
$query_getRecent = "
  SELECT news.post_id, news.title 
  FROM news 
  ORDER BY news.updated 
  DESC LIMIT 10
";
$getRecent = mysql_query($query_getRecent, $mydatabase) or die(mysql_error());
$totalRows_getRecent = mysql_num_rows($getRecent);
while ($row_getRecent = mysql_fetch_assoc($getRecent)){

$rss .= '<item>';
$rss .= '<link> 
  http://localhost/cws_files/index.php?post_id=' . $row_getRecent['post_id'] . '
</link>';
$rss .= '<title>' . $row_getRecent['news.title'] . '</title>';
$rss .= '</item>';

}

$rss .= '</channel>';
$rss .= '</rss>';

echo $rss;
?>
php mysql xml rss rss-reader
3个回答
0
投票

看一下这一行:

$rss .= '<title>' . $row_getRecent['news.title'] . '</title>';

结果数组索引不能包含表名,只能包含列名。替换为

 $rss .= '<title>' . $row_getRecent['title'] . '</title>';

看看你会得到什么。

这条线看起来也很可疑。

$rss .= '<link> 
  http://localhost/cws_files/index.php?post_id=' . $row_getRecent['post_id'] . '
</link>';

检查这是否是单行。否则,如果有换行符,请将其更正为单行。


0
投票
<?php require_once('Connections/mydatabase.php'); ?>
<?php
//header ("Content-Type:text/xml");
?>
<?php

header('Content-type: text/xml');
$mydatabase = mysqli_connect("http://127.0.0.1","root","au291826","db_cms");
$rss = '<?xml version="1.0" encoding="utf-8"?>';
$rss .= '<rss version="2.0">';
$rss .= '<channel>';
$rss .= '<title>My RSS</title>';
$rss .= '<link>http://localhost/cws_files/</link>';
$rss .= '<description>implementation of RSS with PHP </description>';
$rss .= '<language>ar-sa</language>';
$rss .= '<copyright> RSS </copyright>';
mysqli_select_db($database_mydatabase, $mydatabase);
$query_getRecent = "SELECT news.post_id, news.title FROM news ORDER BY news.updated DESC LIMIT 10";
$getRecent = mysqli_query($query_getRecent, $mydatabase) or die(mysqli_error());
$totalRows_getRecent = mysqli_num_rows($getRecent);
while ($row_getRecent = mysqli_fetch_assoc($getRecent)){
$rss .= print_r($row_getRecent);
$rss .= '<item>';
$rss .= '<link> http://localhost/cws_files/index.php?post_id=' . $row_getRecent['post_id'].'</link>';
 $rss .= '<title>' . $row_getRecent['title'] . '</title>';
$rss .= '</item>';

}

$rss .= '</channel>';
$rss .= '</rss>';

echo $rss;
?>

0
投票

您不需要 XML 文档本身中的标题行

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

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
    exit();
}
// Fetch articles from the database
$sql = "SELECT title, link, description, pubDate FROM articles ORDER BY pubDate DESC LIMIT 10";
$stmt = $pdo->query($sql);
$articles = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Set the content type to XML
header('Content-Type: application/rss+xml; charset=utf-8');
// RSS XML structure
echo "<?xml version='1.0' encoding='UTF-8'?>\n";
echo "<rss version='2.0'>\n";
echo "<channel>\n";
echo "<title>Your Site's RSS Feed</title>\n";
echo "<link>https://example.com</link>\n";
echo "<description>This is the RSS feed for Your Site</description>\n";
echo "<language>en-us</language>\n";
echo "<pubDate>" . date(DATE_RSS) . "</pubDate>\n";
foreach ($articles as $article) {
    echo "<item>\n";
    echo "<title>" . htmlspecialchars($article['title']) . "</title>\n";
    echo "<link>" . htmlspecialchars($article['link']) . "</link>\n";
    echo "<description>" . htmlspecialchars($article['description']) . "</description>\n";
    echo "<pubDate>" . date(DATE_RSS, strtotime($article['pubDate'])) . "</pubDate>\n";
    echo "</item>\n";
}
echo "</channel>\n";
echo "</rss>\n";

https://rssatom.com/how_to_create_rss_feed.php

© www.soinside.com 2019 - 2024. All rights reserved.