zoukankan      html  css  js  c++  java
  • sitemap xml文件生成

    sitemap xml生成方法

    
    <?php
    /**
     * SitemapService.php.
     *
     * 生成sitemap
     */
    
    class Sitemap
    {
        public $newLine = "
    ";
        public $indent = " ";
        public $xmlHeader = "<?xml version="1.0" encoding="UTF-8"?>
    ";
        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">
    ";
        public $urlsetValue = "";
        public $urlsetClose = "</urlset>
    ";
    
        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优化

  • 相关阅读:
    读书笔记五
    读书笔记四
    读书笔记3(Teamwork)
    读书笔记二(合格的软件工程师)
    读书笔记1(软件 = 程序 + 工程)
    关于使用Java开发Mis系统
    课堂动手动脑
    Quartz学习
    把数据库中取出的DataTable转换成一个对象 或者对象列表
    SAE上使用cron定时发微博
  • 原文地址:https://www.cnblogs.com/fanfan259/p/10373537.html
Copyright © 2011-2022 走看看