使用 PHP 创建 XML 站点地图
2022-08-30 16:58:08
						我正在尝试创建一个会自动更新的站点地图。我对我的RSS提要做了类似的事情,但这个站点地图拒绝工作。你可以在 http://designdeluge.com/sitemap.xml 实时查看它,我认为主要问题是它无法识别PHP代码。以下是完整源代码:
 <?php 
include 'includes/connection.php';
header("Content-type: text/xml");
echo '<?xml version="1.0" encoding="UTF-8" ?>';
?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">
    <url>
        <loc>http://designdeluge.com/</loc>
        <lastmod>2010-04-20</lastmod>
        <changefreq>weekly</changefreq>
        <priority>1.00</priority>
    </url>
    <url>
        <loc>http://designdeluge.com/about.php</loc>
        <lastmod>2010-04-20</lastmod>
        <changefreq>never</changefreq>
        <priority>0.5</priority>
    </url>
    <?php
    $entries = mysql_query("SELECT * FROM Entries");
    while($row = mysql_fetch_assoc($entries)) {
    $title = stripslashes($row['title']);
    $date = date("Y-m-d", strtotime($row['timestamp']));
    echo "
    <url>
        <loc>http://designdeluge.com/".$title."</loc>
        <lastmod>".$date."</lastmod>
        <changefreq>never</changefreq>
        <priority>0.8</priority>
    </url>";
 } ?>
</urlset>
问题在于动态网址(例如从数据库中提取的网址)没有生成,站点地图也不会进行验证。谢谢!
编辑:现在,我只是想让代码本身工作。我将其设置为本地测试服务器上的PHP文件。正在使用上面的代码。目前,屏幕或源中没有任何内容显示任何内容。我以为我犯了语法错误,但我找不到任何东西。任何和所有的帮助都是值得赞赏的!
编辑2:好吧,我把它整理好了。显然,我必须用PHP回显xml声明。最终代码发布在上面。感谢您的帮助!
 
					 
				 
				    		 
				    		 
				    		 
				    		