Creating multiple Sitemaps in PHP

后端 未结 2 1885
春和景丽
春和景丽 2021-02-10 21:17

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 ent

相关标签:
2条回答
  • 2021-02-10 21:41
    $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
    
    0 讨论(0)
  • 2021-02-10 21:59

    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.

    0 讨论(0)
提交回复
热议问题