微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在PHP中创建多个站点地图

我有以下问题,我在一个数组中生成站点地图的网址.因此该阵列有60000个条目.谷歌要我创建2个站点地图,因为限制是每个站点地图50000个条目.

我怎么能用PHP做这个?我试过了,但是循环停止并输入另一个文件中的其他数据有问题.这是我的代码sofar.

// $data is array with the urls
$count_array = count($data);
$maxlinksinsitemap = 50000;
$numbersofsitemap = ceil($count_array / $maxlinksinsitemap);

for($i = 1; $i <= $numbersofsitemap; $i++) {
    $cfile = "sitemap_" .$i . ".xml";
    $createfile = fopen($cfile, 'w');
    $creat = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    $creat .= "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"\n";
    $creat .= "xmlns:image=\"http://www.sitemaps.org/schemas/sitemap-image/1.1\"\n";
    $creat .= "xmlns:video=\"http://www.sitemaps.org/schemas/sitemap-video/1.1\">\n";
    $creat .= "<url>\n";
    $creat .= "<loc>http://www.urltosite.com</loc>\n";
    $creat .= "<priority>1.00</priority>\n";
    $creat .= "</url>\n";


    $creat .= "</urlset>";  
    fwrite($createfile, $creat);    
    fclose($createfile);


}

我需要一个动态的解决方

感谢帮助.

解决方法:

array_chunk是你的朋友:

$data = array_chunk($data, 50000);

foreach ($data as $key => $value)
{
    $cfile = 'sitemap_' . $i  . '.xml';
    $createfile = fopen($cfile, 'w');

    fwrite($createfile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    fwrite($createfile, "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"\n");
    fwrite($createfile, "xmlns:image=\"http://www.sitemaps.org/schemas/sitemap-image/1.1\"\n");
    fwrite($createfile, "xmlns:video=\"http://www.sitemaps.org/schemas/sitemap-video/1.1\">\n");

    foreach ($value as $url)
    {
        $creat = "<url>\n";
        $creat .= "<loc>" . $url . "</loc>\n";
        $creat .= "<priority>1.00</priority>\n";
        $creat .= "</url>\n";

        fwrite($createfile, $creat);
    }

    fclose($createfile);
}

使用可变数量站点地图开箱即用.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐