icamys / php-sitemap-generator
Simple PHP sitemap generator.
Installs: 250 207
Dependents: 8
Suggesters: 1
Security: 0
Stars: 168
Watchers: 11
Forks: 65
Open Issues: 6
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
Library for sitemap generation and submission.
Features:
- Follows sitemaps.org protocol
- Supports alternative links for multi-language pages (see google documentation)
- Supports video and image sitemap generation
- Low memory usage for any amount of URLs
- Supports sitemap stylesheets
Installation with Composer:
composer require icamys/php-sitemap-generator
Survey
If you found this package useful, please take a short survey to improve your sitemap generation experience.
Usage
<?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();
Video sitemap example
To create video sitemap, pass the $extensions
parameter to the addURL()
method as follows:
<?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. // ...
Image sitemap example
To create image sitemap, pass the $extensions
parameter to the addURL()
method as follows:
<?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. // ...
Testing
Run tests with command:
$ ./vendor/bin/phpunit
Run code coverage:
$ XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-html ./coverage
Changelog
You can find full changelog on the releases page.
Todo
- Remove
$yahooAppId
parameter.