PHPCMS V9 全站Sitemap生成[服务器端版]

需求:
通过服务器的定时任务自动更新网站全站的所有文章,并生成sitemap

以下代码为sitemap crontab for phpcms v9,以下代码保存到phpcms 根目录下,配置cronTab定时生成即可
[php]
<?php
/**
* sitemap.php PHPCMS V9 sitemap入口
*
* @copyright (C) 2005-2011 www.4wei.cn
* @license http://www.4wei.cn
* @lastmodify 2011-12-06
*/
if(PHP_SAPI != ‘cli’)
{
header(‘location: sitemap.xml’);
exit;
}
define(‘SITEMAP_SIZE’, 10000); //sitemap中最大输出数量
define(‘MODEL_SIZE’, 3000); //每个模型中最大输出数量
$model_arr = array(12=>’资讯’, 13=>’评测’, 14=>’教程’); //定义需要生成sitemap的模型

define(‘PHPCMS_PATH’, dirname(__FILE__).DIRECTORY_SEPARATOR);
include PHPCMS_PATH.’/phpcms/base.php’;

$param = pc_base::load_sys_class(‘param’);

$model_arr = empty($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(empty($row) || empty($row[‘url’])) continue;

$date = date("Y-m-d", $row[‘updatetime’]);
$url = str_replace(array(‘&’, ‘\”, ‘"’, ‘>’, ‘<‘), array(‘&amp;’, ‘&apos;’, ‘&quot;’, ‘&gt;’, ‘&lt;’), $row[‘url’]);
$html .= "
<url>
<loc>{$url}</loc>
<lastmod>{$date}</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>";
}

$html .= ‘</urlset>’;
file_put_contents(‘./sitemap.xml’, $html);
?>
[/php]

发表评论

回复 国际特价机票 取消回复

评论列表(6)

  • hallow

    2013.10.2 20:10

    v9的iis 如何设置呢?

    回复
  • 飘云

    2013.5.21 20:05

    请问下:配置cronTab定时生成这一步要怎么操作呢?
    谢谢了。

    搜索linux crontab

    回复
  • 忘仙电脑版

    2013.5.3 18:05

    可以不可以啊?

    回复
  • 国际特价机票

    2012.5.14 23:05

    This XML file does not appear to have any style information associated with it. The document tree is shown below. 怎么解决哦?

    回复
  • 国际机票

    2012.5.14 23:05

    试了一下,不行啊,提示一个错误如下:This XML file does not appear to have any style information associated with it. The document tree is shown below.

    请见:发布接口不能正常使用的常见问题 http://www.4wei.cn/archives/1001577

    回复
  • 动漫周边

    2011.12.9 11:12

    你写得实在是太好了。我唯一能做的,就只有把这个顶上了。

    回复