需求:
通过服务器的定时任务自动更新网站全站的所有文章,并生成sitemaps
以下代码为sitemaps crontab for PHPcms v9,以下代码保存到phpcms 根目录下,配置cronTab定时生成即可
代码:
PHP Code复制内容到剪贴板
- <?php
-
- if(PHP_Sapi != 'cli')
- {
- header('location: sitemaps.xml');
- exit;
- }
- define('SITEMAP_SIZE', 10000);
- define('MODEL_SIZE', 3000);
- $model_arr = array(1=>'资讯', 2=>'图片', 3=>'下载');
-
- define('PHPCMS_PATH', dirname(__FILE__).DIRECTORY_SEPARATOR);
- include PHPCMS_PATH.'/phpcms/base.php';
-
- $param = pc_base::load_sys_class('param');
-
- $model_arr = emptyempty($model_arr) ? getcache('model','commons') : $model_arr;
-
- $map = array();
- foreach ($model_arr as $modelid => $model)
- {
- $tablename = $model['tablename'];
-
- $db = pc_base::load_model('content_model');
- $db->set_model($modelid);
-
- if($db->count() && count($map) < SITEMAP_SIZE)
- {
- $order = 'id desc';
- $arc = $db->select(array('status'=>99), 'url,updatetime', MODEL_SIZE, $order);
- $map = array_merge($map, $arc);
- }
- }
-
-
- $html = '<?xml version="1.0" encoding="utf-8"?>
- <urlset xmlns="Http://www.sitemaps.org/schemas/sitemap/0.9">';
-
- foreach ($map as $row)
- {
- if(emptyempty($row) || emptyempty($row['url'])) continue;
-
- $date = date("Y-m-d", $row['updatetime']);
- $url = str_replace(array('&', '\'', '"', '>', '<'), array('&', ''', '"', '>', '<'), $row['url']);
- $html .= "
- <url>
- <loc>{$url}</loc>
- <lastmod>{$date}</lastmod>
- <changefreq>daily</changefreq>
- <priority>0.8</priority>
- </url>";
- }
-
- $html .= '</urlset>';
- file_put_contents('./sitemaps.xml', $html);
- ?>
当然
windows一样可以使用,通过计划任务与dos命令完成
https://www.jb51
.net/article/86202.htm。
0