1. 程式人生 > >米撲科技的開源項目:sitemap-php 自動生成網站地圖

米撲科技的開源項目:sitemap-php 自動生成網站地圖

about ng- 搜索引擎 end 網站 regular seo anr demo

米撲科技旗下的產品,近期正在做SEO網站優化,其中子需求之一是調研實現了網站地圖(sitemap.xml)

封裝簡化了許多功能模塊,現在分享出來,源代碼可在Github上下載,有簡單的示例。


Github 開源網址sitemap-php


What is sitemap-php ?

sitemap-php 是一個輕量級、簡單快速生成網站地圖的開源項目,由北京米撲科技有限公司(mimvp.com)開發分享。

通過簡單的配置定義,一個函數createSitemap(),可自動生成sitemap.xml、sitemap.html等網站地圖文件,

自動生成的xml、html文件,支持Google、Bing、Baidu等主流搜索引擎收錄。

Fast and lightweight class for generating Google sitemap XML files and index of sitemap files.

Written on PHP and uses XMLWriter extension (wrapper for libxml xmlWriter API) for creating XML files. XMLWriter extension is enabled by default in PHP 5 >= 5.1.2.

If you having more than 50000 url, it splits items to seperated files. (In benchmarks, 1.000.000 url was generating in 8 seconds)

示例:

sitemap.xml : http://mimvp.com/sitemap.xml

sitemap.html : http://mimvp.com/sitemap.html

How to use

Sitemap 封裝了生成sitemap.xml的屬性和方法的類,使用非常簡單,示例代碼:

function testSitemap() {
	$sitemap = new Sitemap("http://mimvp.com");

	 $sitemap->addItem(/, 1.0, daily, Today);
	 $sitemap->addItem(
/hr.php
, 0.8, monthly, time());
$sitemap->addItem(/index.php, 1.0, daily, Jun 25); $sitemap->addItem(/about.php, 0.8, monthly, 2017-06-26); $sitemap->addItem(/hr2.php, 1.0, daily, time())->addItem(/index2.php, 1.0, daily, Today)->addItem(/about2.php, 0.8, monthly, Jun 25); $sitemap->endSitemap(); }
  1. 初始化類對象
$sitemap = new Sitemap("http://mimvp.com");
  1. 添加Item
$sitemap->addItem(/, 1.0, daily, Today);
$sitemap->addItem(/hr.php, 0.8, monthly, time());
$sitemap->addItem(/index.php, 1.0, daily, Jun 25);
$sitemap->addItem(/about.php, 0.8, monthly, 2017-06-26);

或者

$sitemap->addItem(/hr2.php, 1.0, daily, time())->addItem(/index2.php, 1.0, daily, Today)->addItem(/about2.php, 0.8, monthly, Jun 25);
  1. 結束文檔
$sitemap->endSitemap();
  1. 生成結果 sitemap.xml
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
	<url>
		<loc>http://mimvp.com/</loc>
		<priority>1.0</priority>
		<changefreq>daily</changefreq>
		<lastmod>2017-06-26T00:00:00+08:00</lastmod>
	</url>
	<url>
		<loc>http://mimvp.com/hr.php</loc>
		<priority>0.8</priority>
		<changefreq>monthly</changefreq>
		<lastmod>2017-06-26T20:16:23+08:00</lastmod>
	</url>
	<url>
		<loc>http://mimvp.com/index.php</loc>
		<priority>1.0</priority>
		<changefreq>daily</changefreq>
		<lastmod>2017-06-25T00:00:00+08:00</lastmod>
	</url>
	<url>
		<loc>http://mimvp.com/about.php</loc>
		<priority>0.8</priority>
		<changefreq>monthly</changefreq>
		<lastmod>2017-06-26T00:00:00+08:00</lastmod>
	</url>
</urlset>

More Functions

  1. 設置根域名
$sitemap = new Sitemap("http://mimvp.com");

也可以修改初始化的域名為

$sitemap->setDomain(http://blog.mimvp.com);
  1. 設置保存路徑 sitemap.xml默認保存在當前目錄下,也可設置文件夾目錄,例如: xmls/sitemap,表示sitemap.xml保存在當前目錄下的xmls/目錄下,其中xmls目錄會自動創建。註:支持多級目錄
$sitemap->setXmlFile("xmls/sitemap");
$sitemap->setXmlFile("xmls/mimvp/sitemap");
  1. 設置是否更多頭部
$sitemap->setIsChemaMore(true);

如果設置為true,則sitemap.xml文件頭部會增加一些頭部信息:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 	
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" 
  1. 獲取當前寫入的sitemap文件
$sitemap->getCurrXmlFileFullPath();

Advanced Functions

  1. 指定包含文件,以/開頭
$GIncludeArray = array("", "/index.php", "about.php", "hr.php");
  1. 排除特定文件或目錄
$GExcludeArray = array("usercenter/", "sadmin/", "admin/", "sitemap.php");
  1. 遞歸掃描指定目錄,默認掃描三層(可自己設定)
function scanRootPath($rootPath=".", $dirLevel=1, $MaxDirLevel=3, &$resArray=array())
  1. 轉化 xml + xsl 為 html
function createXSL2Html($xmlFile, $xslFile, $htmlFile, $isopen_htmlfile=false) 

Sitemap Demo

  1. 全局變量,G開頭
$GCONFIG = array(	"domain"=>"http://mimvp.com",
			"xmlfile"=>"sitemap",
			"htmlfile"=>"sitemap.html",
			"xslfile"=>"sitemap-xml.xsl",
			"isopen_xmlfile"=>true,
			"isopen_htmlfile"=>true,
			"isscanrootpath"=>true,
			"isxsl2html"=>true,
			"isschemamore"=>true);
  1. 生成sitemap.xml
createSitemap();

生成示例:

技術分享

  1. 生成 sitemap.html
createXSL2Html($xmlFile, $xslFile, $htmlFile, $isopen_htmlfile=false);

生成示例:

技術分享

You need to submit sitemap.xml and sitemap.html to Google、 Bing、 Baidu,etc.

sitemap-php項目,目前支持指定網頁、排除網頁、掃描根目錄等網站地圖;
後期完善時,會增加導出數據庫、爬取整個網站等功能,
也希望您的加入,繼續完善此項目

sitemap-php All Rights by mimvp.com


米撲科技的開源項目:sitemap-php 自動生成網站地圖