如何用php压缩站点地图

如何用php压缩站点地图,php,xml,compression,gzip,sitemap,Php,Xml,Compression,Gzip,Sitemap,我有下面的代码,它工作得很好 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>'.Sit

我有下面的代码,它工作得很好

        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;
浏览器下载一个
.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;
//标题(“内容类型:text/xml”);
标题('content-type:application/x-gzip');
标题('Content-Disposition:attachment;filename=“sitemap.xml.gz”);
$xml='';
$xml.='';
$xml.=''.SiteRoot.daily1.0';
$xml.=''.SiteRoot./directorydaily0.9';
$Query=mysql\u Query(“从`om`按`link`顺序选择链接”);
while($row=mysql\u fetch\u array($Query)){
$xml.='';
$xml.=''.GenerateLink('link',$row['link'])。';
$xml.='weekly';
$xml.='0.8';
$xml.='';
}
$xml.='';
echo$xml;

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


很抱歉,但你为什么要压缩你的网站地图?压缩大型网站地图是一种节省带宽的快捷方式,大多数主要搜索引擎都支持它。@ChibuezeOpata我的网站地图非常大。想想看,你只能将50000个URL放进一个xml网站地图中。事实上,很高兴能提供帮助,我现在也在为我的孩子们做同样的事情sitemap@kamal:您能用gzencode发布最终的sitemap.xml文件吗
    //  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;
echo gzencode($xml);