需求:
通过服务器的定时任务自动更新网站全站的所有文章,并生成sitemaps
以下代码为sitemaps crontab for phpcms v9,以下代码保存到phpcms 根目录下,配置cronTab定时生成即可
代码:
PHP Code复制内容到剪贴板
-
<?php
-
/**
-
* sitemap.php PHPCMS V9 sitemaps入口
-
*
-
* @copyright (C) 2015-2018 piaoyun.cc
-
* @license http://piaoyun.cc
-
* @lastmodify 2015-05-06
-
*/
-
if(PHP_SAPI != 'cli')
-
{
-
header('location: sitemaps.xml');
-
exit;
-
}
-
define('SITEMAP_SIZE', 10000); //sitemaps中最大输出数量
-
define('MODEL_SIZE', 3000); //每个模型中最大输出数量
-
$model_arr = array(1=>'资讯', 2=>'图片', 3=>'下载'); //定义需要生成sitemaps的模型
-
-
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);
-
?>