在 PHP 中创建多个站点地图

发布于 2024-10-18 08:38:54 字数 1107 浏览 4 评论 0原文

我有以下问题,我在数组中生成了站点地图的网址。所以数组有 60000 个条目。谷歌希望我创建 2 个站点地图,因为每个站点地图的限制为 50000 个条目。

我怎样才能用 php 做到这一点?我尝试过,但是循环停止并在其他文件中输入其他数据时遇到问题。到目前为止,这是我的代码。

// $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);


}

我需要一个动态解决方案,

谢谢您的帮助。

i have following problem, i generated the urls for the sitemap, in a array. So the array has 60000 entries. And google wants me to create 2 sitemaps cause the limit is 50000 entries each sitemap.

How can i make this with php? I tried it, but have problems with the loop to stop and enter the other data in the other file. Here is my code 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);


}

I need a dynamic solution,

Thanks for help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

脱离于你 2024-10-25 08:38:54

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);
}

与开箱即用的可变数量的站点地图。

array_chunk is your friend:

$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);
}

Works with a variable number of sitemaps out of the box.

秉烛思 2024-10-25 08:38:54
$count_array = count($data);
$i = 0;

foreach ($data as $entry) {
    if ($i == 0) {
        // code here to start first file
    } else if ($i % 50000 == 0) {
        // code here to end previous file and start next file
    }

    // write entry to current file
    // insert code here....

    // increment counter
    $i++;
}

// code here to end last file
$count_array = count($data);
$i = 0;

foreach ($data as $entry) {
    if ($i == 0) {
        // code here to start first file
    } else if ($i % 50000 == 0) {
        // code here to end previous file and start next file
    }

    // write entry to current file
    // insert code here....

    // increment counter
    $i++;
}

// code here to end last file
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文