无法调试站点地图错误

时间:2015-07-22 20:30:00

标签: xml sitemap

我试图从我的数据库中提取数据并在xml文件中使用它们来生成站点地图。我希望以压缩格式生成10个文件,每个文件包含10个链接。也就是说,file1包含1-10个id,#file1包含11-20个id,...... file10包含91- 100个id。以下是我的代码 -

<?php
$xml = new SimpleXMLElement('<xml/>');
Header('Content-type: text/xml');
$mysql_con = mysqli_connect("localhost", "root", "password", "cases");
for ($i = 1; $i <= 10; $i++) {
    $sql = "SELECT * FROM samplecases LIMIT " . 10*($i-1) . ", 10";
    $result = mysqli_query($mysql_con,$sql);
    while($row = mysqli_fetch_array($result)) {
            $url = $xml->addChild('url');
            $url->addChild('loc', "http://legalcrystal.com/cases/" .$row['id']. "/" . $row['casename']);
            $url->addChild('lastmod',  $row['decidedon']);
    }
    $dom = new DOMDocument('1.0');
    $dom->preserveWhiteSpace = false;
    $dom->formatOutput = true;
    $dom->loadXML($xml->asXML());
    echo $dom->saveXML();
    $dom->save('/var/www/html/es/sitemap/case-'.$i .'.xml');
    $file = "/var/www/html/es/sitemap/case-".$i .".xml";
    $gzfile = "/var/www/html/es/sitemap/case-" .$i .".gz";
    $fp = gzopen ($gzfile, 'w9');
    gzwrite ($fp, file_get_contents($file));
    gzclose($fp);
    unlink($file);
}
?> 

问题是,当我运行代码时,我收到以下错误 -

This page contains the following errors:

error on line 44 at column 6: XML declaration allowed only at the start of the document
Below is a rendering of the page up to the first error.

并且只选择前10个id并且不以xml格式打印。当我查看.gz文件时,file1包含1-10个id,#file包含1-20 id&#39; s,它假设包含11-20 id&#39; s ......... file10包含1-100条记录,它假设包含91-100个id&#39; s。

此外,当我注释掉Header('Content-type: text/xml')行时,我会以无格式的方式在一行中打印所有内容。 有人可以帮助我克服这两个错误吗?

0 个答案:

没有答案