icamys / php-sitemap-generator
简单的PHP站点地图生成器。
5.0.0
2023-10-23 20:29 UTC
Requires
- php: ^8.0
- ext-curl: *
- ext-dom: *
- ext-mbstring: *
- ext-simplexml: *
- ext-spl: *
- ext-xml: *
- ext-xmlwriter: *
- ext-zlib: *
Requires (Dev)
- php-mock/php-mock-phpunit: ^2.7
- phpunit/phpunit: ^9.6
- vimeo/psalm: ^5.15
README
用于生成和提交站点地图的库。
特性
- 遵循sitemaps.org协议
- 支持多语言页面的替代链接(见谷歌文档)
- 支持视频和图像站点地图生成
- 任何数量的URL都占用很少的内存
- 支持站点地图样式表
使用Composer安装
composer require icamys/php-sitemap-generator
调查
如果您觉得这个包很有用,请完成简短调查,以提高您的站点地图生成体验。
用法
<?php include "vendor/autoload.php"; $config = new \Icamys\SitemapGenerator\Config(); // Your site URL. $config->setBaseURL('https://example.com'); // OPTIONAL. Setting the current working directory to be output directory // for generated sitemaps (and, if needed, robots.txt) // The output directory setting is optional and provided for demonstration purposes. // The generator writes output to the current directory by default. $config->setSaveDirectory(sys_get_temp_dir()); // OPTIONAL. Setting a custom sitemap URL base in case if the sitemap files location // is different from the website root. Most of the time this is unnecessary and can be skipped. $config->setSitemapIndexURL('https://example.com/sitemaps/'); $generator = new \Icamys\SitemapGenerator\SitemapGenerator($config); // Create a compressed sitemap $generator->enableCompression(); // Determine how many urls should be put into one file; // this feature is useful in case if you have too large urls // and your sitemap is out of allowed size (50Mb) // according to the standard protocol 50000 urls per sitemap // is the maximum allowed value (see http://www.sitemaps.org/protocol.html) $generator->setMaxURLsPerSitemap(50000); // Set the sitemap file name $generator->setSitemapFileName("sitemap.xml"); // Set the sitemap index file name $generator->setSitemapIndexFileName("sitemap-index.xml"); // Add alternate languages if needed $alternates = [ ['hreflang' => 'de', 'href' => "http://www.example.com/de"], ['hreflang' => 'fr', 'href' => "http://www.example.com/fr"], ]; // Add url components: `path`, `lastmodified`, `changefreq`, `priority`, `alternates` // Instead of storing all urls in the memory, the generator will flush sets of added urls // to the temporary files created on your disk. // The file format is 'sm-{index}-{timestamp}.xml' $generator->addURL('/path/to/page/', new DateTime(), 'always', 0.5, $alternates); // Optional: add sitemap stylesheet. Note that you need to create // the file 'sitemap.xsl' beforehand on your own. $generator->setSitemapStylesheet('sitemap.xsl'); // Flush all stored urls from memory to the disk and close all necessary tags. $generator->flush(); // Move flushed files to their final location. Compress if the option is enabled. $generator->finalize(); // Update robots.txt file in output directory or create a new one $generator->updateRobots(); // Submit your sitemaps to Yandex. $generator->submitSitemap();
视频站点地图示例
要创建视频站点地图,将$extensions
参数传递给addURL()
方法,如下所示
<?php // Initialize the generator // ... // Initialize variable with video tags // For more see the official google documentation: // https://developers.google.com/search/docs/advanced/sitemaps/video-sitemaps $videoTags = [ 'thumbnail_loc' => 'http://www.example.com/thumbs/123.jpg', 'title' => 'Grilling steaks for summer', 'description' => 'Alkis shows you how to get perfectly done steaks every time', 'content_loc' => 'http://streamserver.example.com/video123.mp4', 'player_loc' => 'http://www.example.com/videoplayer.php?video=123', 'duration' => 600, 'expiration_date' => '2021-11-05T19:20:30+08:00', 'rating' => 4.2, 'view_count' => 12345, 'publication_date' => '2007-11-05T19:20:30+08:00', 'family_friendly' => 'yes', 'restriction' => [ 'relationship' => 'allow', 'value' => 'IE GB US CA', ], 'platform' => [ 'relationship' => 'allow', 'value' => 'web mobile', ], 'price' => [ [ 'currency' => 'EUR', 'value' => 1.99, 'type' => 'rent', 'resolution' => 'hd', ] ], 'requires_subscription' => 'yes', 'uploader' => [ 'info' => 'https://example.com/users/grillymcgrillerson', 'value' => 'GrillyMcGrillerson', ], 'live' => 'no', 'tag' => [ "steak", "meat", "summer", "outdoor" ], 'category' => 'baking', ]; $extensions = [ 'google_video' => $videoTags ]; $generator->addURL('/path/to/page/', null, null, null, null, $extensions); // generate, flush, etc. // ...
图像站点地图示例
要创建图像站点地图,将$extensions
参数传递给addURL()
方法,如下所示
<?php // Initialize the generator // ... // Initialize a variable with image tags. // For more see the official google documentation: // https://developers.google.com/search/docs/advanced/sitemaps/image-sitemaps $imageTags = [ 'loc' => 'https://www.example.com/thumbs/123.jpg', 'title' => 'Cat vs Cabbage', 'caption' => 'A funny picture of a cat eating cabbage', 'geo_location' => 'Lyon, France', 'license' => 'https://example.com/image-license', ]; // Alternatively, if you need to pass multiple images per URL, use the format below. // Maximum number of images per URL is 1000. $imageTags = [ [ 'loc' => 'https://www.example.com/thumbs/123.jpg', 'title' => 'Cat vs Cabbage', 'caption' => 'A funny picture of a cat eating cabbage', 'geo_location' => 'Lyon, France', 'license' => 'https://example.com/image-license', ], [ 'loc' => 'https://www.example.com/thumbs/456.jpg', 'title' => 'Dog vs Carrot', 'caption' => 'A funny picture of a dog eating carrot', 'geo_location' => 'Lyon, France', 'license' => 'https://example.com/image-license', ] ]; $extensions = [ 'google_image' => $imageTags ]; $generator->addURL('/path/to/page/', null, null, null, null, $extensions); // generate, flush, etc. // ...
测试
使用命令运行测试
$ ./vendor/bin/phpunit
运行代码覆盖率
$ XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-html ./coverage
变更日志
您可以在发布页面上找到完整的变更日志。
待办事项
- 移除
$yahooAppId
参数。