百度站长 sitemap

手册

实现流程

  1. 按照百度文档生成指定格式文件
  2. 写入文件到自己项目路径下
  3. 写定时脚本抓取新URL到项目路径下
  4. 注意:涉及多台服务器的情况:用rsync(linux),同步数据

流程代码

(1) 生成sitemap XML

';
            $sitemapContent .= '';
            foreach ($sitemapInfo as $key => $value) {
                $sitemapContent .= '';
                $sitemapContent .= "{$value['url']}";
                $sitemapContent .= "";
                $sitemapContent .= "{$value['lastmod']}";
                $sitemapContent .= "{$value['changefreq']}";
                $sitemapContent .= "{$value['priority']}";
                $sitemapContent .= '';
                unset($sitemapInfo[$key], $key, $value);
            }
            $sitemapContent .= '';
        }
        return $sitemapContent;
    }

    /**
     * 生成sitemap索引内容
     * @param  array $sitemapIndexInfo
     * 参数1:loc,该页的网址。该值必须少于256个字节(必填项)。
     * 参数2:lastmod,该文件上次修改的日期(选填项)。格式为年-月-日
     * @return string
     */
    public static function createSitemapIndexContents(array $sitemapIndexInfo)
    {
        $sitemapIndexContent = '';
        if (!empty($sitemapIndexInfo) && is_array($sitemapIndexInfo)) {
            $sitemapIndexContent .= '';
            foreach ($sitemapIndexInfo as $key => $value) {
                $sitemapIndexContent .= '';
                $sitemapIndexContent .= "{$value['url']}";
                $sitemapIndexContent .= "{$value['lastmod']}";
                $sitemapIndexContent .= '';
                unset($sitemapIndexInfo[$key], $key, $value);
            }
            $sitemapIndexContent .= '';
        }
        return $sitemapIndexContent;
    }
}

(2) Sitemap Controller 结合自身逻辑,拼URL

getSinglePageURLs('home');
        $this->createBaiduSitemap('single', $sitemapInfo, 'home');
        //详情列表
        $sitemapInfo = $this->getArticlePageURLs();
        $this->createBaiduSitemap('article', $sitemapInfo, 'article');


//      sitemapIndex是区别于xml的另一种提交方式
        $this->createBaiduSitemapIndex();
    }

    /**
     * 获取所有单页面的URL地址,例如首页
     */
    private function getSinglePageURLs($type)
    {
        $sitemapInfo = array();
        $singlePage = array(
            'home' => array('' => '1.0', 'hot/' => '0.8', 'city/' => '0.6', 'price/' => '0.6', 'news/' => '0.6', 'guide/' => '0.6', 'reviews/' => '0.6'), //首页 行情列
        );
        foreach ($singlePage[$type] as $pageUrl => $priority) {
            $sitemapInfo[] = $this->getSitemapInfo($pageUrl, $priority, $type);
        }
        return $sitemapInfo;
    }


    private function getSitemapInfo($url, $priority, $type)
    {
        $preUrl = self::DOMAIN_HOME_URL;
        $freq = 'always';

        return array(
            'url' => $preUrl . $url,
            'lastmod' => date('Y-m-d'),
            'changefreq' => $freq,
            'priority' => $priority
        );
    }

    /**
     * 获取所有文章页面的URL
     */
    private function getArticlePageURLs()
    {
        $mongo = MongoTool::getDB('content', true)->selectCollection('article_info');
        $sitemapInfo = array();
        $data = $mongo->find();
        foreach ($data as $key => $value) {
            $url = $this->getArticleUrl($value);
            $sitemapInfo[] = $this->getSitemapInfo($url, '0.3', "article"); //文章页
        }
        return $sitemapInfo;
    }


    /**
     * 创建百度Sitemap文件
     */
    private function createBaiduSitemapFile($sitemapContents, $filePath)
    {
        if (!empty($sitemapContents) && !empty($filePath)) {
            // 创建文件
            file_put_contents(self::FILE_DIR . $filePath, $sitemapContents);

            // 为SitemapIndex文件记录生成过哪些Sitemap文件
            $this->sitemapFilesInfo[] = array(
                'url' => self::DOMAIN_HOME_URL . $filePath,
                'lastmod' => date('Y-m-d'),
            );
        }
    }

    /**
     * 创建百度Sitemap索引文件
     */
    private function createBaiduSitemapIndexFile($sitemapIndexContents, $filePath)
    {
        if (!empty($sitemapIndexContents) && !empty($filePath)) {
            // 创建文件
            file_put_contents(self::FILE_DIR . $filePath, $sitemapIndexContents);
        }
    }

    /**
     * 创建百度Sitemap
     */
    private function createBaiduSitemap($fileName, array $sitemapInfo, $save = 'all')
    {

        if (!empty($sitemapInfo) && is_array($sitemapInfo)) {
            $total = count($sitemapInfo);
            $pages = ceil($total / self::LIMIT_NUM);
            for ($i = 1; $i <= $pages; $i++) {
                $offset = self::LIMIT_NUM * ($i - 1);
                $partSitemapInfo = array_slice($sitemapInfo, $offset, self::LIMIT_NUM);

                $sitemapContents = Tool_BaiduSitemap::createSitemapContents($partSitemapInfo);
                unset($partSitemapInfo);
                $filePath = "m_{$fileName}_{$i}.xml";
                $this->createBaiduSitemapFile($sitemapContents, $filePath);
            }
            unset($sitemapInfo);
        }
    }

    /**
     * 创建百度SitemapIndex
     */
    private function createBaiduSitemapIndex()
    {
        // 拆分成2个方法是为了可以进一步处理哪些类型的页面需要生成到SitemapIndex文件中
        $sitemapFilesInfo = $this->sitemapFilesInfo;
        $pcSitemapFilesIndexInfo = $mSitemapFilesIndexInfo = array();

        if (!empty($sitemapFilesInfo) && is_array($sitemapFilesInfo)) {
            foreach ($sitemapFilesInfo as $info) {
                if (!stripos($info['url'], 'm.')) {
                    $pcSitemapFilesIndexInfo[] = $info;
                } else {
                    $mSitemapFilesIndexInfo[] = $info;
                }
            }

//            $this->createBaiduSitemapIndexFile(Tool_BaiduSitemap::createSitemapIndexContents($pcSitemapFilesIndexInfo), 'index.xml');
            $this->createBaiduSitemapIndexFile(Tool_BaiduSitemap::createSitemapIndexContents($mSitemapFilesIndexInfo), 'm_index.xml');
        }
    }

    //todo 优化
    function carConfig()
    {
        $db_car["hostname"] = "localhost";
        $db_car["username"] = "xxx";
        $db_car["password"] = "xxx";
        $db_car["database"] = "xxxx";
        $db_car["charset"] = "utf8";
        $db_car["pconnect"] = 0;
        $db_car["log"] = 0;
        $db_car["logfilepath"] = './';
        return $db_car;
    }

}

(3) linux写定时任务

  • 命令行:crontab -e
  • 参数1:定时
  • 参数2 路径
  • 参数3 执行程序
* */1 * * * cd /home/xxxx/xxx/xxxx/data; php create_xxxxx_sitemap.php

每小时执行一次创建sitemap的程序


一个敲代码,写文字的人,我在这里!

百度站长 sitemap_第1张图片
来玩啊

你可能感兴趣的:(百度站长 sitemap)