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格式

谷歌seo优化

sitemap xml文件生成的更多相关文章

  1. XML文件生成C++代码(基于rapidxml)

    简述 与XML文件生成C++代码(基于pugixml)中的功能一致,只是这里改用的rapidxml来实现.就不多说了,直接放代码. 代码 #include "rapidxml-1.13/ra ...

  2. XML文件生成C++代码(基于pugixml)

    简述 在一个项目中需要用到XML的解析和生成,知乎上有人推荐rapidxml和pugixml等库.RapidXML一看库还比较大,就先研究一下pugixml了. 因为对解析XML的需求不大(都是一些很 ...

  3. 根据xml文件生成javaBean

    原 根据xml文件生成javaBean 2017年08月15日 18:32:26 吃完喝完嚼益达 阅读数 1727 版权声明:本文为博主原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出 ...

  4. WebAPI使用多个xml文件生成帮助文档

    一.前言 上篇有提到在WebAPI项目内,通过在Nuget里安装(Microsoft.AspNet.WebApi.HelpPage)可以根据注释生成帮助文档,查看代码实现会发现是基于解析项目生成的xm ...

  5. WebAPI使用多个xml文件生成帮助文档(转)

    http://www.cnblogs.com/idoudou/p/xmldocumentation-for-web-api-include-documentation-from-beyond-the- ...

  6. 如何引用XML文件生成C#类

    目录 XSD File Generate Class File Simply. 1 Why use XSD file to create C# classes?... 2 How to convert ...

  7. Android(java)学习笔记185:xml文件生成

    1.xml文件: 用元素描述数据,跨平台. 2.利用传统的方式创建xml文件,下面是一个案例: 设计思路:建立一个学生管理系统,创建xml文件保存学生信息: (1)首先是布局文件activity_ma ...

  8. 根据XML文件生成XSD文件

    在.net开发环境中查找XSD.exe文件,比如我的在C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin目录下,将该路径添加到Path中,打开控制台,到 ...

  9. eclips运行generatorConfig.xml文件生成代码

    描述: 如何通过eclips工具来运行 generatorConfig.xml 文件来自动生成代码并获取数据(类似于mybaits逆向生成)? mybatis-generator:generate 2 ...

随机推荐

  1. mybatis和spring整合的关键配置

    spring配置文件 applicationContext.xml: <beans xmlns="http://www.springframework.org/schema/beans ...

  2. windows系统实现mysql数据库数据库主从复制

    环境: master mysql服务器 192.168.8.201 slave mysql服务器 192.168.8.89 目标: 实现主从复制 1.将MySQL5.5安装文件分别拷贝到两台机器的c盘 ...

  3. Linux中 Lua 访问Sql Server的配置方法

    一.背景说明: 通过lua脚本实现对SQL Server数据库的操作. 二.具体设定: 安装流程图:如果是使用Lua连接SQL Server,从上到下则需要安装lua -> luaSQL-ODB ...

  4. 关于报错stale element reference: element is not attached to the page document处理

    1.现象 在执行脚本时,有时候引用一些元素对象会抛出如下异常 org.openqa.selenium.StaleElementReferenceException: stale element ref ...

  5. Day6------------磁盘用满的两种情况

    1.文件包含元数据和写入的内容 元数据:存在硬盘中的inode ls -i /etc/passwd.bak 查看inode df -i 查看inode 2.磁盘用满的两种情况 1).内容太多 2).空 ...

  6. 100以内奇偶数(for循环)

  7. select 禁止 选择

    禁止select控件選擇 大家知道, 對於HTML控件select, 是沒有readOnly屬性的,所以設置它並不起作用,如: <select id="select1" re ...

  8. PHP服务端支付宝支付及回调

    支付宝支付 (由app端自行调起支付宝/微信) 1.下载PHP版SDK 1 <?php 2 3 define('IN_ECS', true); 4 5 /*App支付 PHP服务端*/ 6 /* ...

  9. jsTree动态加载数据

    Views代码 @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="view ...

  10. hdu 2112 map+Dijkstra

    无向图 用map 起点和终点可能一样 数组不能开太大 WA了好多发 Sample Input6xiasha westlake //起点 终点xiasha station 60xiasha Shoppi ...