米撲科技旗下的產品,近期正在做SEO網站最佳化,其中子需求之一是調研實現了網站地圖(sitemap.xml)
封裝簡化了許多功能模組,現在分享出來,原始碼可在Github上下載,有簡單的樣本。
Github 開源網址: sitemap-php
What is sitemap-php ?
sitemap-php 是一個輕量級、簡單快速產生網站地圖的開源項目,由北京米撲科技有限公司(mimvp.com)開發分享。
通過簡單的配置定義,一個函數createSitemap(),可自動產生sitemap.xml、sitemap.html等網站地圖檔案,
自動產生的xml、html檔案,支援Google、Bing、Baidu等主流搜尋引擎收錄。
Fast and lightweight class for generating Google sitemap XML files and index of sitemap files.
Written on PHP and uses XMLWriter extension (wrapper for libxml xmlWriter API) for creating XML files. XMLWriter extension is enabled by default in PHP 5 >= 5.1.2.
If you having more than 50000 url, it splits items to seperated files. (In benchmarks, 1.000.000 url was generating in 8 seconds) 樣本: sitemap.xml : http://mimvp.com/sitemap.xml sitemap.html : http://mimvp.com/sitemap.html How to use
Sitemap 封裝了產生sitemap.xml的屬性和方法的類,使用非常簡單,範例程式碼:
function testSitemap() {$sitemap = new Sitemap("http://mimvp.com"); $sitemap->addItem('/', '1.0', 'daily', 'Today'); $sitemap->addItem('/hr.php', '0.8', 'monthly', time()); $sitemap->addItem('/index.php', '1.0', 'daily', 'Jun 25'); $sitemap->addItem('/about.php', '0.8', 'monthly', '2017-06-26'); $sitemap->addItem('/hr2.php', '1.0', 'daily', time())->addItem('/index2.php', '1.0', 'daily', 'Today')->addItem('/about2.php', '0.8', 'monthly', 'Jun 25'); $sitemap->endSitemap();} 初始化類對象
$sitemap = new Sitemap("http://mimvp.com"); 添加Item
$sitemap->addItem('/', '1.0', 'daily', 'Today');$sitemap->addItem('/hr.php', '0.8', 'monthly', time());$sitemap->addItem('/index.php', '1.0', 'daily', 'Jun 25');$sitemap->addItem('/about.php', '0.8', 'monthly', '2017-06-26');
或者
$sitemap->addItem('/hr2.php', '1.0', 'daily', time())->addItem('/index2.php', '1.0', 'daily', 'Today')->addItem('/about2.php', '0.8', 'monthly', 'Jun 25'); 結束文檔
$sitemap->endSitemap();
產生結果 sitemap.xml
<?xml version="1.0" encoding="UTF-8"?><urlset 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" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>http://mimvp.com/</loc><priority>1.0</priority><changefreq>daily</changefreq><lastmod>2017-06-26T00:00:00+08:00</lastmod></url><url><loc>http://mimvp.com/hr.php</loc><priority>0.8</priority><changefreq>monthly</changefreq><lastmod>2017-06-26T20:16:23+08:00</lastmod></url><url><loc>http://mimvp.com/index.php</loc><priority>1.0</priority><changefreq>daily</changefreq><lastmod>2017-06-25T00:00:00+08:00</lastmod></url><url><loc>http://mimvp.com/about.php</loc><priority>0.8</priority><changefreq>monthly</changefreq><lastmod>2017-06-26T00:00:00+08:00</lastmod></url></urlset>
More Functions 設定根網域名稱
$sitemap = new Sitemap("http://mimvp.com");
也可以修改初始化的網域名稱為
$sitemap->setDomain('http://blog.mimvp.com'); 設定儲存路徑 sitemap.xml預設儲存在目前的目錄下,也可設定檔案夾目錄,例如: xmls/sitemap,表示sitemap.xml儲存在目前的目錄下的xmls/目錄下,其中xmls目錄會自動建立。註:支援多級目錄
$sitemap->setXmlFile("xmls/sitemap");$sitemap->setXmlFile("xmls/mimvp/sitemap"); 設定是否更多頭部
$sitemap->setIsChemaMore(true);
如果設定為true,則sitemap.xml檔案頭部會增加一些頭部資訊:
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"
擷取當前寫入的sitemap檔案
$sitemap->getCurrXmlFileFullPath();
Advanced Functions 指定包含檔案,以/開頭