首页> 实战笔录 >PHP开发笔记 >ThinkPHP ThinkPHP

TP6 TP5一键生成sitemap站点地图索引文件 代码分享

作者:小萝卜 2021-05-26 浏览 1352

简介TP6 TP5 PHP一键生成sitemap站点地图索引文件 代码分享,注意:以下代码是源生调用,接入tp请放在model 或者 extra 或者extends下面,注意加下namespace,然后调用就行勒。

注意:以下代码是源生调用,接入tp请放在model 或者 extra 或者extends下面,注意加下namespace,然后调用就行勒。

调用代码:
 
/**
 * @author	Sandy <sandy@mimvp.com>
 * @copyright	2009-2017 mimvp.com
 * @version	1.0.1 (2017.06.20)
 */


$xmlFile = 'sitemap_index.xml';
echo "<br> xmlFile : $xmlFile <br>";

$sitemap = new SitemapIndex($xmlFile);
// $sitemap->setUseGzip(true);

$sitemap->addSitemap('http://mimvp.com/sitemap.xml');
$sitemap->addSitemap('http://mimvp.com/sitemap-2.xml', time()-1000000);
$sitemap->addSitemap('http://mimvp.com/sitemap-3.xml', '2017-06-22
');
$sitemap->endSitemap();

echo "<script>window.open('" . $xmlFile . "')</script>";
echo "<br>Create SitemapIndex Success<br>";

类文件 Sitemap.php
 
/**
 * SitemapIndex
 *
 * 生成 Google Sitemaps index (sitemap_index.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 SitemapIndex
{
	private $writer;
	private $filePath;
	private $useGzip = false;
	
	public function __construct($filePath)
	{
		$this->filePath = $filePath;
	}
	
	public function getFilePath()
	{
		return $this->filePath;
	}
	
	private function createSitemap()
	{
		$this->writer = new XMLWriter();
		$this->writer->openMemory();
		$this->writer->startDocument('1.0', 'UTF-8');
		$this->writer->setIndent(true);
		$this->writer->startElement('sitemapindex');
		$this->writer->writeAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
	}
	
	public function addSitemap($location, $lastModified = null)
	{
		if ($this->writer === null) {
			$this->createSitemap();
		}
		
		$this->writer->startElement('sitemap');
		$this->writer->writeElement('loc', $location);
		$this->writer->writeElement('lastmod', $this->getLastModifiedDate($lastModified));
		$this->writer->endElement();
	}
	
	public function endSitemap()
	{
		if ($this->writer instanceof XMLWriter) {
			$this->writer->endElement();
			$this->writer->endDocument();
			$filePath = $this->getFilePath();
// 			if ($this->useGzip) {
// 				$filePath = 'compress.zlib://' . $filePath;
// 			}
			file_put_contents($filePath, $this->writer->flush());
		}
	}
	
	public function setUseGzip($value)
	{
		if ($value && !extension_loaded('zlib')) {
			throw new \RuntimeException('Zlib extension must be installed to gzip the sitemap.');
		}
		$this->useGzip = $value;
	}
	
	private function getLastModifiedDate($date=null) {
		if(null == $date) {
			$date = time();
		}
		if (ctype_digit($date)) {
			return date('c', $date);
		} else {
			$date = strtotime($date);
			return date('c', $date);	// Y-m-d
		}
	}
}

很赞哦! (1)

文章评论

    共有1条评论 来说两句吧...

    验证码: captcha

      2021-06-27 16:37:09 北京北京网友

      这回复的验证码是真尼玛恶心,呵呵,这是给人看的验证码?

    高端网站建设