PHP生成sitemap.xml地图函数_php实例

复制代码 代码如下:

<?php

/**
 *    网站地图更新控制器
 *
 *    @author    Garbin
 *    @usage    none
 */
class SitemapApp extends FrontendApp
{
    function __construct()
    {
        $this->SitemapApp();
    }
    function SitemapApp()
    {
        parent::__construct();
        $this->_google_sitemmap_file = ROOT_PATH . '/data/google_sitemmap.xml';
    }

    function index()
    {
        if (!Conf::get('sitemap_enabled'))
        {
            return;
        }
        $from = empty($_GET['from']) ? 'google' : trim($_GET['from']);
        switch ($from)
        {
            case 'google':
                $this->_output_google_sitemap();
            break;
        }
    }

    /**
     *    输出Google sitemap
     *
     *    @author    Garbin
     *    @return    void
     */
    function _output_google_sitemap()
    {
        header("Content-type: application/xml");
        echo $this->_get_google_sitemap();
    }

    /**
     *    获取Google sitemap
     *
     *    @author    Garbin
     *    @return    string
     */
    function _get_google_sitemap()
    {
        $sitemap = "";
        if ($this->_google_sitemap_expired())
        {
            /* 已过期,重新生成 */

            /* 获取有更新的项目 */
            $updated_items = $this->_get_updated_items($this->_get_google_sitemap_lastupdate());

            /* 重建sitemap */
            $sitemap = $this->_build_google_sitemap($updated_items);

            /* 写入文件 */
            $this->_write_google_sitemap($sitemap);
        }
        else
        {
            /* 直接返回旧的sitemap */
            $sitemap = file_get_contents($this->_google_sitemmap_file);
        }

        return $sitemap;
    }

    /**
     *    判断Google sitemap是否过期
     *
     *    @author    Garbin
     *    @return    boolean
     */
    function _google_sitemap_expired()
    {
        if (!is_file($this->_google_sitemmap_file))
        {
            return true;
        }
        $frequency = Conf::get('sitemap_frequency') * 3600;
        $filemtime = $this->_get_google_sitemap_lastupdate();

        return (time() >= $filemtime + $frequency);
    }

    /**
     *    获取上次更新日期
     *
     *    @author    Garbin
     *    @return    int
     */
    function _get_google_sitemap_lastupdate()
    {
        return is_file($this->_google_sitemmap_file) ? filemtime($this->_google_sitemmap_file) : 0;
    }

    /**
     *    获取已更新的项目
     *
     *    @author    Garbin
     *    @return    array
     */
    function _get_updated_items($timeline = 0)
    {
        $timeline && $timeline -= date('Z');
        $limit = 5000;
        $result = array();
        /* 更新的店铺 */
        $model_store =& m('store');
        $updated_store = $model_store->find(array(
            'fields'    => 'store_id, add_time',
            'conditions' => "add_time >= {$timeline} AND state=" . STORE_OPEN,
            'limit'     => "0, {$limit}",
        ));

        if (!empty($updated_store))
        {
            foreach ($updated_store as $_store_id => $_v)
            {
                $result[] = array(
                    'url'       => SITE_URL . '/index.php?app=store&id=' . $_store_id,
                    'lastmod'   => date("Y-m-d", $_v['add_time']),
                    'changefreq'=> 'daily',
                    'priority'  => '1',
                );
            }
        }
        /* 更新的文章 */
        $model_article =& m('article');
        $updated_article = $model_article->find(array(
            'fields'    => 'article_id, add_time',
            'conditions'=> "add_time >= {$timeline} AND if_show=1",
            'limit'     => "0, {$limit}",
        ));
        if (!empty($updated_article))
        {
            foreach ($updated_article as $_article_id => $_v)
            {
                $result[] = array(
                    'url'       => SITE_URL . '/index.php?app=article&act=view&article_id=' . $_article_id,
                    'lastmod'   => date("Y-m-d", $_v['add_time']),
                    'changefreq'=> 'daily',
                    'priority'  => '0.8',
                );
            }
        }

        /* 更新的商品 */
        $model_goods =& m('goods');
        $updated_goods = $model_goods->find(array(
            'fields'        => 'goods_id, last_update',
            'conditions'    => "last_update >= {$timeline} AND if_show=1 AND closed=0",
            'limit'         => "0, {$limit}",
        ));
        if (!empty($updated_goods))
        {
            foreach ($updated_goods as $_goods_id => $_v)
            {
                $result[] = array(
                    'url'       => SITE_URL . '/index.php?app=goods&id=' . $_goods_id,
                    'lastmod'   => date("Y-m-d", $_v['last_update']),
                    'changefreq'=> 'daily',
                    'priority'  => '0.8',
                );
            }
        }

        return $result;
    }

    /**
     *    生成Google sitemap
     *
     *    @author    Garbin
     *    @param     array $items
     *    @return    string
     */
    function _build_google_sitemap($items)
    {
        $sitemap = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\r\n";
        $sitemap .= "    <url>\r\n        <loc>" . htmlentities(SITE_URL, ENT_QUOTES) . "</loc>\r\n        <lastmod>" . date('Y-m-d', gmtime()) . "</lastmod>\r\n        <changefreq>always</changefreq>\r\n        <priority>1</priority>\r\n    </url>";
        if (!empty($items))
        {
            foreach ($items as $item)
            {
                $sitemap .= "\r\n    <url>\r\n        <loc>" . htmlentities($item['url'], ENT_QUOTES) . "</loc>\r\n        <lastmod>{$item['lastmod']}</lastmod>\r\n        <changefreq>{$item['changefreq']}</changefreq>\r\n        <priority>{$item['priority']}</priority>\r\n    </url>";
            }
        }
        $sitemap .= "\r\n</urlset>";

        return $sitemap;
    }

    /**
     *    写入Google sitemap文件
     *
     *    @author    Garbin
     *    @param     string $sitemap
     *    @return    void
     */
    function _write_google_sitemap($sitemap)
    {
        file_put_contents($this->_google_sitemmap_file, $sitemap);
    }
}

?>

时间: 2024-08-24 18:01:16

PHP生成sitemap.xml地图函数_php实例的相关文章

PHP+Mysql实现多关键字与多字段生成SQL语句的函数_php技巧

本文实例讲述了PHP+Mysql实现多关键字与多字段生成SQL语句的函数的方法.分享给大家供大家参考.具体实现方法如下: 先看实例: 复制代码 代码如下: $keyword="1 2 3"; echo $sql=search($keyword,"enter_gongyin_pic","a+b+c"); //函数生成,没有LIMIT,没有ORDER BY 生成: 复制代码 代码如下: SELECT * FROM `enter_gongyin_pic

php xml Parser函数应用实例

xml 函数是 php 核心的组成部分.无需安装即可使用这些函数. php xml parser 函数 php:指示支持该函数的最早的 php 版本. 函数 描述 php utf8_decode() 把 utf-8 字符串解码为 iso-8859-1. 3 utf8_encode() 把 iso-8859-1 字符串编码为 utf-8. 3 xml_error_string() 获取 xml 解析器的错误描述. 3 xml_get_current_byte_index() 获取 xml 解析器的

浅谈php fopen下载远程文件的函数_php实例

如下所示: //下载附件 function get_file($url, $folder = "./") { set_time_limit (24 * 60 * 60); // 设置超时时间 $destination_folder = $folder . '/'; // 文件下载保存目录,默认为当前文件目录 if (!is_dir($destination_folder)) { // 判断目录是否存在 mkdirs($destination_folder); // 如果没有就建立目录

浅谈PHP检查数组中是否存在某个值 in_array 函数_php实例

PHP in_array() 函数检查数组中是否存在某个值,如果存在则返回 TRUE ,否则返回 FALSE . 语法: bool in_array( mixed needle, array array [, bool strict] ) 参数说明: 参数 说明 needle 需要在数组中搜索的值,如果是字符串,则区分大小写 array 需要检索的数组 strict 可选,如果设置为 TRUE ,则还会对 needle 与 array 中的值类型进行检查 例子: <?php $arr_a = a

PHP中使用smarty生成静态文件的例子_php实例

首先先要把需要静态化的内容填充到模版中去 复制代码 代码如下: #eg.这个是静态化首页的 function staticIndex(){ $newslist = $article->getArticles(null,54,'DESC',1,6,false,1,2,'',0,0,1);   if($newslist){    foreach($newslist as $k=>$v){     $newslist[$k]['title_all'] = $v['title'];     $news

分享自定义的几个PHP功能函数_php实例

最近不是在折腾论坛嘛,各种类各种函数,原创一些,从别人那儿qiang过来一些,在此分享出来,希望有朋友能用的到~ 注意:部分函数可能不够完善,由此造成的漏洞风险自担~ 提交过滤 function filter($text) { //完全过滤注释 $text = preg_replace('/<!--?.*-->/', '', $text); //完全过滤js $text = preg_replace('/<script?.*\/script>/', '', $text); //过滤

PHP在网页中动态生成PDF文件详细教程_php实例

本文详细介绍使用 PHP 动态构建 PDF 文件的整个过程.使用免费 PDF 库 (FPDF) 或 PDFLib-Lite 等开源工具进行实验,并使用 PHP 代码控制 PDF 内容格式. 有时您需要准确控制要打印的页面的呈现方式.在这种情况下,HTML 就不再是最佳选择了.PDF 文件使您能够完全控制页面的呈现方式,以及文本.图形和图像在页面上的呈现方式.遗憾的是,用来构建 PDF 文件的 API 不属于 PHP 工具包的标准部件.现在您需要提供一点帮助. 当您在网络上搜索,寻找对 PHP 的

WordPress中使主题支持小工具以及添加插件启用函数_php实例

让主题支持小工具WordPress 的小工具(widget)是一大特色,它让用户自由拖动组合内容,而且任何插件和主题都可以添加一个额外的小工具,增加扩展性. 默认情况下,一个主题并不会支持小工具,需要主题开发者启用小工具功能并把小工具在相应的前台位置调用出来,这样用户才能在后台直接拖动生成侧边栏. 本文就来教你如何激活小工具功能,并且添加一个侧边栏,最后在前台显示出来. 注册侧边栏 默认的,后台外观下是没有 "小工具" 这个菜单按钮的,如果想要让他出现,就至少需要注册一个侧边栏,否则即

php生成与读取excel文件_php实例

在网站中经常会生成表格,CSV和Excel都是常用的报表格式,CSV相对来说比较简单,如果大家有疑问我会相继发布一些CSV的实例,这里主要介绍用PHP来生成和读取Excel文件. 要执行下面的函数,首先要引入一个类库:PHPExcel,PHPExcel是一个强大的PHP类库,用来读写不同的文件格式,比如说Excel 2007,PDF格式,HTML格式等等,这个类库是建立在Microsoft's OpenXML和PHP 的基础上的,对Excel提供的强大的支持,比如设置工作薄,字体样式,图片以及边