首页 > 解决方案 > 动态 XML 站点地图脚本出错

问题描述

我的动态 XML 脚本有问题。我不断收到XML Parsing Error: XML or text declaration not at start of entity错误,并尝试了几乎所有在这里找到的东西,但没有任何效果。有任何想法吗?

编码:

<?php include("connect.php");
$query = "SELECT `website_url` FROM `websites`";
$result = mysqli_query($conn, $query);
$base_url = "http://localhost/test/www.shopperapproved.com/";
header("Content-Type: application/xml; charset=utf-8");
echo '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">' . PHP_EOL;
while($row = mysqli_fetch_array($result)) {
echo '<url>' . PHP_EOL;
echo '<loc>'.$base_url. $row["website_url"] .'/</loc>' . PHP_EOL;
echo '<changefreq>daily</changefreq>' . PHP_EOL;
echo '</url>' . PHP_EOL;
}
echo '</urlset>' . PHP_EOL;

标签: xml

解决方案


我解决了我的问题。刚刚移动了这两行:

header("Content-Type: application/xml; charset=utf-8");
echo '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;

在文件的顶部include("connect.php");


推荐阅读