0

如何使用 PHP 制作一个完全简单的静态 RSS 提要?

为什么这不起作用:

<?xml version="1.0"?>
<rss version=\"2.0\">
<channel>

<?php echo "<title>The Channel Title Goes Here</title>"; ?>
<description>The explanation of how the items are related goes here</description>
<link>http://www.directoryoflinksgohere</link>

<item>
<title>The Title Goes Here</title>
<description>The description goes here</description>
<link>http://www.linkgoeshere.com</link>
</item>

<item>
<title>Another Title Goes Here</title>
<description>Another description goes here</description>
<link>http://www.anotherlinkgoeshere.com</link>
</item>

</channel>
</rss>

我当然需要使用 mysql 自动更新它,但现在我只需要知道我什至可以如何将 php 与 RSS 结合使用。

这实际上会导致 RSS 文件下载而不是显示:

<?php

echo "<?xml version=\"1.0\"?>
<rss version="2.0">
<channel>

<title>The Channel Title Goes Here</title>
<description>The explanation of how the items are related goes here</description>
<link>http://www.directoryoflinksgohere</link>

<item>
<title>The Title Goes Here</title>
<description>The description goes here</description>
<link>http://www.linkgoeshere.com</link>
</item>

<item>
<title>Another Title Goes Here</title>
<description>Another description goes here</description>
<link>http://www.anotherlinkgoeshere.com</link>
</item>

</channel>
</rss>";

?>

更新:

这将正确输出 RSS。几乎。变量的值不是写的,而是变量的名称,例如它将写“$toast”而不是写“YES”。

<?php

$counter = 0;

$con=mysqli_connect("localhost","username","password","database");
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql = mysqli_query($con,"SELECT name FROM people");


while($row = mysqli_ffetch_array($sql)){
$test[$counter] = $row['name'];
$counter++;
}

$toast = "YES";


echo "

header(\"Content-Type: application/rss+xml; charset=ISO-8859-1\")

<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
<rss version="2.0">

<channel>

<title>The Channel Title Goes Here</title>
<description>$test[0]</description>
<link>http://www.directoryoflinksgohere</link>

<item>
<title>The Title Goes Here</title>
<description>$toast</description>
<link>http://www.linkgoeshere.com</link>
</item>

<item>
<title>Another Title Goes Here</title>
<description>Another description goes here</description>
<link>http://www.anotherlinkgoeshere.com</link>
</item>

</channel>
</rss>";

?>
4

2 回答 2

0

你似乎走在正确的轨道上。要做的重要事情之一是设置正确的标头以指示内容类型(应用程序/rss+xml)。此外,该<?xml version="1.0"?>标签会导致 PHP 出现问题,因此更容易做到echo这一点。比如,<?php echo "<?xml version=\"1.0\"?>"; ?>

于 2013-06-22T14:55:32.810 回答
0

添加具有以下内容的 .htaccess 文件:

RewriteEngine On
RewriteRule ^sitemaps.xml$ /sitemap.php [L]

使用您发布的代码创建 sitemap.php

完毕

于 2013-06-22T14:52:11.053 回答