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 ...
随机推荐
- mybatis二级缓存应用及与ehcache整合
mybaits的二级缓存是mapper范围级别,除了在SqlMapConfig.xml设置二级缓存的总开关,还要在具体的mapper.xml中开启二级缓存. 1.开启mybatis的二级缓存 在核心配 ...
- 最全Kafka 设计与原理详解【2017.9全新】
一.Kafka简介 1.1 背景历史 当今社会各种应用系统诸如商业.社交.搜索.浏览等像信息工厂一样不断的生产出各种信息,在大数据时代,我们面临如下几个挑战: 如何收集这些巨大的信息 如何分析它 如何 ...
- Ant+Jmeter自动化接口测试的部署 及 部署过程中的坑
一.环境准备: 1.Jdk1.6或以上:http://www.oracle.com/technetwork/java/javase/downloads/index.html 配置环境变量-系统变 ...
- MySQL数据库排序选择的作用和该如何选择编码格式
前言:在创建数据库的时候,会有这样一个选项->排序规则,平时在创建数据库的时候并没有注意,只是选择了默认,也没感觉有什么问题,今天看到这个突然好奇起来,所以看了一些资料做了以下的一些总结,若有错 ...
- GuzzleHttp 请求设置超时时间
之前调用一个三方的 WEB API,大量的请求超时,导致 PHP 进程被占用完.整个网站一直报 504. 其中一个优化措施就是对三方 API 调用设置超时时间. use GuzzleHttp\Clie ...
- poj1511,线段树扫描线面积
经典题,线段树扫描线其实类似区间更新,一般的做法是想象一根扫描线从上扫到下或者从左扫到右,本题的做法是从上扫到下 只要扫到了一根水平线,就将其更新到线段树对应区间中,区间和它的子区间是独立更新的 #i ...
- 一个判断男女性别的JS脚本
var xb= "男" if(xb=="男"){ document.write("你是男生") }else{ document.write( ...
- Oracle GoldenGate常用配置端口
1 简介 Oracle Golden Gate软件是一种基于日志的结构化数据复制备份软件,它通过解析源数据库在线日志或归档日志获得数据的增量变化,再将这些变化应用到目标数据库,从而实现源数据库与目标数 ...
- 编译Caffe出错,解决方案记录
1.This file was generated by an older version of protoc which is python环境和Anaconda python环境可能存在冲突. 使 ...
- Tr A HDU1575
矩阵基本算法 #include<cstdio> using namespace std; int n; struct matrix { int m[15][15]; }ans,base; ...