如何使用 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>";
?>