如何用php压缩网站地图


how to compress a sitemap with php

我有下面的代码,它可以很好地工作

        header ("content-type: text/xml");
        $xml = '<?xml version="1.0" encoding="UTF-8"?>';
        $xml .= '<urlset xmlns="http://www.google.com/schemas/sitepam/0.84">';
        $xml .= '<url><loc>'.SiteRoot.'</loc><changefreq>daily</changefreq><priority>1.0</priority></url>';
        $xml .= '<url><loc>'.SiteRoot.'/directory</loc><changefreq>daily</changefreq><priority>0.9</priority></url>';
        $Query = mysql_query ("SELECT link FROM `om` ORDER BY `link`");
        while($row = mysql_fetch_array($Query)) {
            $xml .= '<url>';
            $xml .= '<loc>'.GenerateLink( 'link',$row['link'] ).'</loc>';
            $xml .= '<changefreq>weekly</changefreq>';
            $xml .= '<priority>0.8</priority>';
            $xml .= '</url>';
        }
        $xml .= '</urlset>';
        echo $xml;

当我试图用mime头压缩它时

header('content-type: application/x-gzip');
header('Content-Disposition: attachment; filename="sitemap.xml.gz"');

浏览器下载了一个.gz文件,但它没有打开。winrar给我一个错误,说:档案要么是未知格式,要么是损坏的

这是最后一个代码:

    //  header ("content-type: text/xml");
        header('content-type: application/x-gzip');
        header('Content-Disposition: attachment; filename="sitemap.xml.gz"');
        $xml = '<?xml version="1.0" encoding="UTF-8"?>';
        $xml .= '<urlset xmlns="http://www.google.com/schemas/sitepam/0.84">';
        $xml .= '<url><loc>'.SiteRoot.'</loc><changefreq>daily</changefreq><priority>1.0</priority></url>';
        $xml .= '<url><loc>'.SiteRoot.'/directory</loc><changefreq>daily</changefreq><priority>0.9</priority></url>';
        $Query = mysql_query ("SELECT link FROM `om` ORDER BY `link`");
        while($row = mysql_fetch_array($Query)) {
            $xml .= '<url>';
            $xml .= '<loc>'.GenerateLink( 'link',$row['link'] ).'</loc>';
            $xml .= '<changefreq>weekly</changefreq>';
            $xml .= '<priority>0.8</priority>';
            $xml .= '</url>';
        }
        $xml .= '</urlset>';
        echo $xml;

尝试使用一些内置的gzip函数,如gzencode

echo gzencode($xml);