sitemap xml文件生成
sitemap xml生成方法
<?php
/**
* SitemapService.php.
*
* 生成sitemap
*/
class Sitemap
{
public $newLine = "\n";
public $indent = " ";
public $xmlHeader = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
public $urlsetOpen = "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"
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\">\n";
public $urlsetValue = "";
public $urlsetClose = "</urlset>\n";
public $rootUrl = "";
public function __construct($rootUrl = "https://www.xxx")
{
if (empty($rootUrl)) {
$rootUrl = "https://www.xxx";
}
$this->rootUrl = $rootUrl;
}
private function makeUrlString ($urlString) {
return htmlentities($urlString, ENT_QUOTES|ENT_XML1, 'UTF-8');
}
/**
* 返回格式: 2019-02-01T12:40:05+00:00
* 参数格式: 2019-02-01 12:40:05
* @param $dateTime
* @return bool|string
*/
private function makeIso8601TimeStamp ($dateTime) {
if (!$dateTime) {
$dateTime = date('Y-m-d H:i:s');
}
if (is_numeric(substr($dateTime, 11, 1))) {
$isoTS = substr($dateTime, 0, 10) ."T"
.substr($dateTime, 11, 8) ."+00:00";
} else {
$isoTS = substr($dateTime, 0, 10);
}
return $isoTS;
}
private function makeUrlTag ($url, $modifiedDateTime, $changeFrequency, $priority) {
$newLine = $this->newLine;
$indent = $this->indent;
$urlOpen = "$indent<url>$newLine";
$urlClose = "$indent</url>$newLine";
$locOpen = "$indent$indent<loc>";
$locClose = "</loc>$newLine";
$lastmodOpen = "$indent$indent<lastmod>";
$lastmodClose = "</lastmod>$newLine";
$changefreqOpen = "$indent$indent<changefreq>";
$changefreqClose = "</changefreq>$newLine";
$priorityOpen = "$indent$indent<priority>";
$priorityClose = "</priority>$newLine";
$urlTag = $urlOpen;
$urlValue = $locOpen .$this->makeUrlString($url) .$locClose;
if ($modifiedDateTime) {
$urlValue .= $lastmodOpen .$this->makeIso8601TimeStamp($modifiedDateTime) .$lastmodClose;
}
if ($changeFrequency) {
$urlValue .= $changefreqOpen .$changeFrequency .$changefreqClose;
}
if ($priority) {
$urlValue .= $priorityOpen .$priority .$priorityClose;
}
$urlTag .= $urlValue;
$urlTag .= $urlClose;
return $urlTag;
}
/**
* 验证是否符合url格式
* url format: /xxx|lastmod|changefreq|priority
*
* @param $pageLine
* @return bool
*/
private function validateEntry ($pageLine) {
$page = explode("|", $pageLine);
$url = trim($page[0]);
$processLine = TRUE;
if (substr($url, 0, 1) != "/") {
$processLine = FALSE;
}
return $processLine;
}
/**
* 获取待生成的链接
* url format: /xxx|lastmod|changefreq|priority
*
* @return array
*/
private function getUrls()
{
$lastmod = date('Y-m-d H:i:s');
$changefreq = "daily";
$priority_index = "1.0";
$priority_category = "0.8";
$priority_details = "0.5";
$pageList = [];
$pageList[] = "/|$lastmod|$changefreq|$priority_index";
// 添加自己的网站url
$pageList[] = "/ranking|$lastmod|$changefreq|$priority_category";
return $pageList;
}
/**
* 生成sitemap文件
*/
public function generate()
{
$pageList = $this->getUrls();
foreach($pageList as $pageLine) {
$processLine = $this->validateEntry ($pageLine);
$page = explode("|", $pageLine);
$url = trim($page[0]);
if ($processLine) {
$this->urlsetValue .= $this->makeUrlTag ($this->rootUrl .$url, trim($page[1]), trim($page[2]), trim($page[3]));
}
}
return $this->xmlHeader . $this->urlsetOpen . $this->urlsetValue . $this->urlsetClose;
}
}
/**
* 1. Sitemap 构造方法,默认的rootUrl 修改为待整理的网站域名
* 2. Sitemap getUrls方法,添加自己网站的url
* 3. 调用Sitemap时,rootUrl的参数修改
*/
$rootUrl = "";
$sitemap = new Sitemap($rootUrl);
$xml_data = $sitemap->generate();
header('Content-type: application/xml; charset="utf-8"');
echo $xml_data;
参考链接
sitemap xml文件生成的更多相关文章
- XML文件生成C++代码(基于rapidxml)
简述 与XML文件生成C++代码(基于pugixml)中的功能一致,只是这里改用的rapidxml来实现.就不多说了,直接放代码. 代码 #include "rapidxml-1.13/ra ...
- XML文件生成C++代码(基于pugixml)
简述 在一个项目中需要用到XML的解析和生成,知乎上有人推荐rapidxml和pugixml等库.RapidXML一看库还比较大,就先研究一下pugixml了. 因为对解析XML的需求不大(都是一些很 ...
- 根据xml文件生成javaBean
原 根据xml文件生成javaBean 2017年08月15日 18:32:26 吃完喝完嚼益达 阅读数 1727 版权声明:本文为博主原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出 ...
- WebAPI使用多个xml文件生成帮助文档
一.前言 上篇有提到在WebAPI项目内,通过在Nuget里安装(Microsoft.AspNet.WebApi.HelpPage)可以根据注释生成帮助文档,查看代码实现会发现是基于解析项目生成的xm ...
- WebAPI使用多个xml文件生成帮助文档(转)
http://www.cnblogs.com/idoudou/p/xmldocumentation-for-web-api-include-documentation-from-beyond-the- ...
- 如何引用XML文件生成C#类
目录 XSD File Generate Class File Simply. 1 Why use XSD file to create C# classes?... 2 How to convert ...
- Android(java)学习笔记185:xml文件生成
1.xml文件: 用元素描述数据,跨平台. 2.利用传统的方式创建xml文件,下面是一个案例: 设计思路:建立一个学生管理系统,创建xml文件保存学生信息: (1)首先是布局文件activity_ma ...
- 根据XML文件生成XSD文件
在.net开发环境中查找XSD.exe文件,比如我的在C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin目录下,将该路径添加到Path中,打开控制台,到 ...
- eclips运行generatorConfig.xml文件生成代码
描述: 如何通过eclips工具来运行 generatorConfig.xml 文件来自动生成代码并获取数据(类似于mybaits逆向生成)? mybatis-generator:generate 2 ...
随机推荐
- 关于windows2008r2系统80端口被system进程占用的问题
80端口被system占用的问题 今天启动tomcat的时候发现无法启动80端口被占用 通过netstat -ano查看,发现被pid=4的进程占用 检查进程发现是system进程pid=4给占用 ...
- MyBatis返回Map键值对数据
List<Map<String, String>> getMtypeList(); <select id="getMtypeList" resultT ...
- 在jsp页面,将小数转换为百分比
<fmt:formatNumber type="number" value="${temp.illegalCount*100/temp.unitCount}&quo ...
- 13-JS中的面向对象
创建对象的几种常用方式 1.使用Object或对象字面量创建对象 2.工厂模式创建对象 3.构造函数模式创建对象 4.原型模式创建对象 1.使用Object或对象字面量创建对象 JS中最基本创建对象的 ...
- C#面向对象(基础知识)
面向对象:就是CLASS,class就是用户自定义类型: class:用户自定义引用类型:三大特点:封装.继承.多态: 解决方案中右键添加class:class内可以写结构体,枚举,函数: C#中各个 ...
- xcode 8 清除无用的打印
OS_ACTIVITY_MODE disable 虽然模拟器这样写能屏蔽掉无用的打印,但是在真机测试的时候什么都不会打印 Nslog 也打印不出来 , 这时候就要点掉 OS_ACTIVIT ...
- redis介绍以及安装
一.redis介绍 redis是一个key-value存储系统.和Memcached类似,它支持存储的values类型相对更多,包括字符串.列表.哈希散列表.集合,有序集合. 这些数据类型都支持pus ...
- Java String str = new String(value)和String str = value区别
示例代码: public class StringDemo2 { public static void main(String[] args) { String s1 = new String(&qu ...
- DOM编程艺术推荐的addLoadEvent和insertAfter
addLoadEvent.js function addLoadEvent(func){ var oldonLoad = window.onload; if(typeof window.onload! ...
- php输出json的内容
$json = '{"foo": 12345}'; $obj = json_decode($json); print $obj->{'foo'}; // 12345