i have following problem, i generated the urls for the sitemap after url 50K this code wont make new sitemap like sitemap2.xml or sitemap3.xml
header('Content-type: application/xml; charset="ISO-8859-1"', true);
$dataAll1 = scandir('cache-data');
unset($dataAll1[0]);
unset($dataAll1[1]);
unset($dataAll1[2]);
$sitemap = '<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="'.PROTOCOL.'://www.sitemaps.org/schemas/sitemap/0.9">';
$sitemap .= '<url>
<loc>' . SITE_HOST . '</loc>
<priority>1.0</priority>
</url>';
foreach($dataAll1 as $val){
$data = json_decode(file_get_contents('cache-data/'.$val),1);
if($val=='index.php'){
continue;
}
$sitemap .= '<url>
<loc>'.SITE_HOST . '/blog/' . $data['jk'].'jktk'.$data['tk'] . '-' . $service->slugify($data['title']).'</loc>
<priority>0.9</priority>
<changefreq>daily</changefreq>
</url>';
}
$sitemap.='</urlset>';
echo $sitemap;
how to write the correct code for the code above so that you can create a new sitemap after reaching 50K urls?
all help is very valuable
CodePudding user response:
It may help to solve your problem. please have a look below code
define('PROTOCOL', 'url');
$sitemapStart = '<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="'.PROTOCOL.'://www.sitemaps.org/schemas/sitemap/0.9">';
$sitemapEnd ='</urlset>';
$maxLength= 30;
$fileName = 'sitemap';$filenameInc = 0;
$inc = 0;
$sitemapList = '';
for ($i=1; $i <= 100; $i )
{
$sitemapList .= '
<url>
<loc>URL</loc>
<priority>0.9</priority>
<changefreq>daily</changefreq>
</url>';
# code...
if ($maxLength == $inc || $i == 100)
{
$fileName = "sitemap";
if ($filenameInc > 0)
{
$fileName .= $filenameInc;
}
$fileName .= ".php";
$sitemap = $sitemapStart . $sitemapList .$sitemapEnd;
// echo $sitemap;
if(!file_put_contents($fileName, $sitemap))
{
// overwriting the file failed (permission problem maybe), debug or log here
}
$inc = 0;
$filenameInc ;
$sitemapList = '';
}
else {
$inc ;
}
}
CodePudding user response:
Separate out the xml generation code into it's own function like so:
function createSiteMap($data) {
$sitemap = '<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="'.PROTOCOL.'://www.sitemaps.org/schemas/sitemap/0.9">\n';
$sitemap .= '\t<url>\n\t\t<loc>' . SITE_HOST . '</loc>\n\t\t<priority>1.0</priority>\n\t</url>\n';
foreach($data as $val) {
$data = json_decode(file_get_contents('cache-data/'.$val),1);
if($val=='index.php') continue;
$sitemap .= '\t<url>\n';
$sitemap .= '\t\t<loc>'.SITE_HOST . '/blog/' . $data['jk'].'jktk'.$data['tk'] . '-' . $service->slugify($data['title']).'</loc>\n';
$sitemap .= '\t\t<priority>0.9</priority>\n';
$sitemap .= '\t\t<changefreq>daily</changefreq>\n';
$sitemap .= '\t</url>\n';
}
$sitemap.='</urlset>\n';
return $sitemap
}
Then you can iterate over your array and slice it up how you like:
$dataAll1 = scandir('cache-data');
unset($dataAll1[0]);
unset($dataAll1[1]);
unset($dataAll1[2]);
for ($i = 0; $i < $dataAll1; $i = $i 50000) {
$partial = array_slice($dataAll1, $i, 50000); // What if there isn't 50,000 elements left in the array?
$result = createSiteMap($partial);
}