alkree / sitemap
网站地图和网站地图索引生成器
2.2.3
2020-01-17 13:54 UTC
Requires
- php: >=5.3.0
- ext-xmlwriter: *
Requires (Dev)
- phpunit/phpunit: ~4.4
README
网站地图和网站地图索引生成器。
功能
- 创建网站地图文件:普通或gzip压缩。
- 创建多语言网站地图文件。
- 创建网站地图索引文件。
- 当URL限制或文件大小限制达到时,自动创建新文件。
- 快速且内存高效。
安装
通过Composer安装非常简单
composer require alkree/sitemap
之后,确保您的应用程序通过包含vendor/autoload.php
来自动加载Composer类。
如何使用它
use alkree\sitemap\Sitemap; use alkree\sitemap\Index; // create sitemap $sitemap = new Sitemap(__DIR__ . '/sitemap.xml'); // add some URLs $sitemap->addItem('http://example.com/mylink1'); $sitemap->addItem('http://example.com/mylink2', time()); $sitemap->addItem('http://example.com/mylink3', time(), Sitemap::HOURLY); $sitemap->addItem('http://example.com/mylink4', time(), Sitemap::DAILY, 0.3); // write it $sitemap->write(); // get URLs of sitemaps written $sitemapFileUrls = $sitemap->getSitemapUrls('http://example.com/'); // create sitemap for static files $staticSitemap = new Sitemap(__DIR__ . '/sitemap_static.xml'); // add some URLs $staticSitemap->addItem('http://example.com/about'); $staticSitemap->addItem('http://example.com/tos'); $staticSitemap->addItem('http://example.com/jobs'); // write it $staticSitemap->write(); // get URLs of sitemaps written $staticSitemapUrls = $staticSitemap->getSitemapUrls('http://example.com/'); // create sitemap index file $index = new Index(__DIR__ . '/sitemap_index.xml'); // add URLs foreach ($sitemapFileUrls as $sitemapUrl) { $index->addSitemap($sitemapUrl); } // add more URLs foreach ($staticSitemapUrls as $sitemapUrl) { $index->addSitemap($sitemapUrl); } // write it $index->write();
多语言网站地图
use alkree\sitemap\Sitemap; // create sitemap // be sure to pass `true` as second parameter to specify XHTML namespace $sitemap = new Sitemap(__DIR__ . '/sitemap_multi_language.xml', true); // Set URL limit to fit in default limit of 50000 (default limit / number of languages) $sitemap->setMaxUrls(25000); // add some URLs $sitemap->addItem('http://example.com/mylink1'); $sitemap->addItem([ 'ru' => 'http://example.com/ru/mylink2', 'en' => 'http://example.com/en/mylink2', ], time()); $sitemap->addItem([ 'ru' => 'http://example.com/ru/mylink3', 'en' => 'http://example.com/en/mylink3', ], time(), Sitemap::HOURLY); $sitemap->addItem([ 'ru' => 'http://example.com/ru/mylink4', 'en' => 'http://example.com/en/mylink4', ], time(), Sitemap::DAILY, 0.3); // write it $sitemap->write();
选项
有方法可以配置Sitemap
实例
setMaxUrls($number)
。设置单个文件中要写入的最大URL数量。默认是50000,这是根据规范和大多数现有实现确定的限制。setMaxBytes($number)
。设置单个网站地图文件的最大大小。默认是10MiB,应与大多数当前搜索引擎兼容。setBufferSize($number)
。设置在写入文件之前要保留在内存中的URL数量。默认是10。较大的值提供边际效益。另一方面,当文件大小限制达到时,必须将完整的缓冲区写入下一个文件。setUseIndent($bool)
。设置XML是否应该缩进。默认是true。setUseGzip($bool)
。设置生成的网站地图文件是否应该gzip压缩。默认是false
。使用此功能必须启用zlib
扩展。
有方法可以配置Index
实例
setUseGzip($bool)
。设置生成的索引文件是否应该gzip压缩。默认是false
。使用此功能必须启用zlib
扩展。
运行测试
为了运行测试,请执行以下命令
composer install
./vendor/bin/phpunit