在 PHP 中创建多个站点地图
我有以下问题,我在数组中生成了站点地图的网址。所以数组有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
array_chunk
是您的朋友:与开箱即用的可变数量的站点地图。
array_chunk
is your friend:Works with a variable number of sitemaps out of the box.