Chinaunix首页 | 论坛 | 博客
  • 博客访问: 43054
  • 博文数量: 9
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 107
  • 用 户 组: 普通用户
  • 注册时间: 2017-08-07 18:38
个人简介

米扑,小而美,简爱 mimvp.com

文章分类

全部博文(9)

文章存档

2019年(1)

2018年(3)

2017年(5)

我的朋友

分类: PHP

2017-08-07 18:48:05

米扑科技旗下的产品,近期正在做SEO网站优化,其中子需求之一是调研实现了网站地图(sitemap.xml)

封装简化了许多功能模块,现在分享出来,源代码可在Github上下载,有简单的示例。


Github 开源网址: 




What is sitemap-php ?


sitemap- 是一个轻量级、简单快速生成网站地图的开源项目,由北京米扑科技有限公司()开发分享。

通过简单的配置定义,一个函数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  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 : 

sitemap.html : 

How to use

Sitemap 封装了生成sitemap.xml的属性和方法的类,使用非常简单,示例代码:

function testSitemap() {  $sitemap = new Sitemap("");    $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("");
  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="" xsi:schemaLocation=" /sitemap.xsd" xmlns="">
	<url>
		<loc>/loc>
		<priority>1.0priority>
		<changefreq>dailychangefreq>
		<lastmod>2017-06-26T00:00:00+08:00lastmod>
	url>
	<url>
		<loc>/hr.phploc>
		<priority>0.8priority>
		<changefreq>monthlychangefreq>
		<lastmod>2017-06-26T20:16:23+08:00lastmod>
	url>
	<url>
		<loc>/index.phploc>
		<priority>1.0priority>
		<changefreq>dailychangefreq>
		<lastmod>2017-06-25T00:00:00+08:00lastmod>
	url>
	<url>
		<loc>/about.phploc>
		<priority>0.8priority>
		<changefreq>monthlychangefreq>
		<lastmod>2017-06-26T00:00:00+08:00lastmod>
	url>
urlset>

More Functions

  1. 设置根域名
$sitemap = new Sitemap("");

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

$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="" 	
xsi:schemaLocation=" /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"=>"",  "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


推荐到极客头条
阅读(1663) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:Shell curl 和 wget 使用代理IP

给主人留下些什么吧!~~