加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

thinkphp自动生成sitemap网站站点地图

发布时间:2020-12-14 13:55:37 所属栏目:大数据 来源:网络整理
导读:首先用的是 米扑科技的开源项目:sitemap-php 自动生成网站地图 使用方式可以去github上看文档??https://github.com/mimvp/mimvp-sitemap-php function testSitemap() { $sitemap = new appextraMysitemap("http://mimvp.com" ); $sitemap -addItem('/','

首先用的是

米扑科技的开源项目:sitemap-php 自动生成网站地图

使用方式可以去github上看文档??https://github.com/mimvp/mimvp-sitemap-php

function testSitemap() {
    $sitemap = new appextraMysitemap("http://mimvp.com");
    
     $sitemap->addItem('/','1.0','daily','Today');
     $sitemap->addItem('/hr.php','0.8','monthly',time());
     $sitemap->addItem('/index.php','Jun 25'$sitemap->addItem('/about.php','2017-06-26');
     
     $sitemap->addItem('/hr2.php',1)">time())->addItem('/index2.php','Today')->addItem('/about2.php',1)">$sitemap->endSitemap();
}

?

?

后台效果是自己做的

?

?

核心代码:

首先在extra目录下引入类文件

?

?Mysitemap.php

<?php
namespace appextra;

use XMLWriter;

/**
 * Sitemap
 *
 * 生成 Google Sitemap files (sitemap.xml)
 *
 * @package    Sitemap
 * @author     Sandy <sandy@mimvp.com>
 * @copyright  2009-2017 mimvp.com
 * @license    http://opensource.org/licenses/MIT MIT License
 * @link       http://github.com/mimvp/sitemap-php
 */
class Mysitemap {

    private $writer;        // XMLWriter对象
    $domain = "http://mimvp.com";             网站地图根域名
    $xmlFile = "sitemap";                     网站地图xml文件(不含后缀.xml)
    $xmlFileFolder = "";                     网站地图xml文件夹
    $currXmlFileFullPath = "";                 网站地图xml文件当前全路径
    $isSchemaMore= true;                     网站地图是否添加额外的schema
    $current_item = 0;                         网站地图item个数(序号)
    $current_sitemap = 0;                     网站地图的个数(序号)

    const SCHEMA_XMLNS = 'http://www.sitemaps.org/schemas/sitemap/0.9';
    const SCHEMA_XMLNS_XSI = 'http://www.w3.org/2001/XMLSchema-instance'const SCHEMA_XSI_SCHEMALOCATION = 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd'const DEFAULT_PRIORITY = 0.5const SITEMAP_ITEMS = 50000const SITEMAP_SEPERATOR = '-'const INDEX_SUFFIX = 'index'const SITEMAP_EXT = '.xml';

    *
     * @param string $domain    :    初始化网站地图根域名
     */
    public function __construct($domain) {
        $this->setDomain();
    }

    *
     * 设置网站地图根域名,开头用 http:// or https://,结尾不要反斜杠/
     * @param string $domain    :    网站地图根域名 <br>例如: http://mimvp.com
     function setDomain() {
        if(substr($domain,-1) == "/") {
            $domain = strlen($domain)-1);
        }
        $this->domain = ;
        return $this;
    }

    *
     * 返回网站根域名
     private  getDomain() {
        $this->domain;
    }

    *
     * 设置网站地图的xml文件名
     function setXmlFile($xmlFile$base = basename();
        $dir = dirname();
        if(!is_dir($dir)) {
            $res = mkdir(iconv("UTF-8","GBK",$dir),0777,1)">true);
            if($res) {
echo "mkdir $dir success";
            } else {
echo "mkdir $dir fail.";
            }
        }
        $this->xmlFile = *
     * 返回网站地图的xml文件名
      getXmlFile() {
        xmlFile;
    }

    function setIsChemaMore($isSchemaMore$this->isSchemaMore =  getIsSchemaMore() {
        isSchemaMore;
    }

    *
     * 设置XMLWriter对象
     function setWriter(XMLWriter $writer$this->writer = *
     * 返回XMLWriter对象
      getWriter() {
        writer;
    }

    *
     * 返回网站地图的当前item
     * @return int
      getCurrentItem() {
        current_item;
    }

    *
     * 设置网站地图的item个数加1
      incCurrentItem() {
        $this->current_item = $this->current_item + 1*
     * 返回当前网站地图(默认50000个item则新建一个网站地图)
     * @return int
      getCurrentSitemap() {
        current_sitemap;
    }

    *
     * 设置网站地图个数加1
      incCurrentSitemap() {
        $this->current_sitemap = $this->current_sitemap + 1 getXMLFileFullPath() {
        $xmlfileFullPath = ""if (getCurrentSitemap()) {
            $xmlfileFullPath = $this->getXmlFile() . self::SITEMAP_SEPERATOR . $this->getCurrentSitemap() . self::SITEMAP_EXT;     第n个网站地图xml文件名 + -n + 后缀.xml
        }  {
            $this->getXmlFile() . self::SITEMAP_EXT;     第一个网站地图xml文件名 + 后缀.xml
        }
        $this->setCurrXmlFileFullPath($xmlfileFullPath);         保存当前xml文件全路径
        $xmlfileFullPath getCurrXmlFileFullPath() {
        currXmlFileFullPath;
    }

    function setCurrXmlFileFullPath($currXmlFileFullPath$this->currXmlFileFullPath = *
     * Prepares sitemap XML document
      startSitemap() {
        $this->setWriter(new XMLWriter());
        $this->getWriter()->openURI($this->getXMLFileFullPath());     获取xml文件全路径

        $this->getWriter()->startDocument('1.0','UTF-8'$this->getWriter()->setIndentString("t"$this->getWriter()->setIndent($this->getWriter()->startElement('urlset'getIsSchemaMore()) {
            $this->getWriter()->writeAttribute('xmlns:xsi',self::SCHEMA_XMLNS_XSI);
            $this->getWriter()->writeAttribute('xsi:schemaLocation',1)">SCHEMA_XSI_SCHEMALOCATION);
        }
        $this->getWriter()->writeAttribute('xmlns',1)">SCHEMA_XMLNS);
    }

    *
     * 写入item元素,url、loc、priority字段必选,changefreq、lastmod可选
     function addItem($loc,1)">$priority = self::DEFAULT_PRIORITY,1)">$changefreq = NULL,1)">$lastmod = NULLif (($this->getCurrentItem() % self::SITEMAP_ITEMS) == 0) {
            getWriter() instanceof XMLWriter) {
                endSitemap();
            }
            startSitemap();
            incCurrentSitemap();
        }
        incCurrentItem();
        $this->getWriter()->startElement('url'$this->getWriter()->writeElement('loc',1)">$this->getDomain() . $loc);             必选
        $this->getWriter()->writeElement('priority',1)">$priority);                     必选
        $changefreq$this->getWriter()->writeElement('changefreq',1)">$changefreq);             可选
        }
        $lastmod$this->getWriter()->writeElement('lastmod',1)">$this->getLastModifiedDate($lastmod));    $this->getWriter()->endElement();
        *
     * 转义时间格式,返回时间格式为 2016-09-12
     function getLastModifiedDate($date=nullnull == $date$date = ();
        }
        if (ctype_digit()) {
            return date('c',1)">$date);     Y-m-d
        } strtotime();
        }
    }

    *
     * 结束网站xml文档,配合开始xml文档使用
      endSitemap() {
        if (!getWriter()) {
            startSitemap();
        }
        endElement();
        endDocument();
        flush();
    }

    *
     * Writes Google sitemap index for generated sitemap files
     *
     * @param string $loc Accessible URL path of sitemaps
     * @param string|int $lastmod The date of last modification of sitemap. Unix timestamp or any English textual datetime description.
     function createSitemapIndex($lastmod = 'Today'$indexwriter =  XMLWriter();
        $indexwriter->openURI($this->getXmlFile() . self::SITEMAP_SEPERATOR . self::INDEX_SUFFIX . self::SITEMAP_EXT);
        $indexwriter->startDocument('1.0',1)">$indexwriter->setIndent($indexwriter->startElement('sitemapindex'$indexwriter->writeAttribute('xmlns:xsi',1)">SCHEMA_XMLNS_XSI);
        $indexwriter->writeAttribute('xsi:schemaLocation',1)">SCHEMA_XSI_SCHEMALOCATION);
        $indexwriter->writeAttribute('xmlns',1)">SCHEMA_XMLNS);
        for ($index = 0; $index < $this->getCurrentSitemap(); $index++$indexwriter->startElement('sitemap');
            $indexwriter->writeElement('loc',1)">$loc . $this->getFilename() . ($index ? self::SITEMAP_SEPERATOR . $index : '') . self::SITEMAP_EXT);
            $indexwriter->writeElement('lastmod',1)">));
            $indexwriter->endElement();
        }
        endDocument();
    }

}

?

然后控制器里使用

?

endSitemap();
}

?

我操作的时候遇到个坑,就是thinkphp报错:XMLWriter 没找到

一开始我以为是没有装XMLWriter扩展,phpinfo发现是有的,后来就在类文件Mysitemap.php,头部加上

namespace appextra;

use XMLWriter;

就可以了、

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读