PHP Sitemap Generator
Library for sitemap generation and submission.
Internally uses SplFixedArrays, thus is faster and uses less memory then alternatives.
Features:
- Follows sitemaps.org protocol
- Supports alternative links for multi-language pages (see google docs)
Usage example:
<?php
include "src/SitemapGenerator.php";
// Setting the document root and the output path relative to it
// robots.txt will always be created at the document root (if needed)
// sitemaps are saved in the output directory
// by default all files are written to the path defined as $rootPath
$rootPath = $_SERVER['DOCUMENT_ROOT'];
// relative path
$outputDir = '';
$generator = new \Icamys\SitemapGenerator\SitemapGenerator('https://www.example.com', $outputDir);
// will create also compressed (gzipped) sitemap
$generator->toggleGZipFileCreation();
// 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 is maximum value (see http://www.sitemaps.org/protocol.html)
$generator->setMaxURLsPerSitemap(50000);
// sitemap file name
$generator->setSitemapFileName("sitemap.xml");
// sitemap index file name
$generator->setSitemapIndexFileName("sitemap-index.xml");
// alternate languages
$alternates = [
['hreflang' => 'de', 'href' => "http://www.example.com/de"],
['hreflang' => 'fr', 'href' => "http://www.example.com/fr"],
];
// adding url `loc`, `lastmodified`, `changefreq`, `priority`, `alternates`
$generator->addURL('/url/path/', new DateTime(), 'always', 0.5, $alternates);
// generate internally a sitemap
$generator->createSitemap();
// write early generated sitemap to file(s)
$generator->writeSitemap();
// update robots.txt file in output directory or create a new one
$generator->updateRobots($rootPath);
// submit your sitemaps to Google, Yahoo, Bing and Ask.com
// optional: provide Yahoo AppID to avoid hitting limits when reporting sitemap to Yahoo
$generator->submitSitemap($yahooAppID = '');
Testing
Run tests with command:
$ ./vendor/bin/phpunit
Run code coverage:
$ ./vendor/bin/phpunit --coverage-html ./coverage
Changelog
New in 2.0.0:
- Major code rework
- No more public properties in generator, using only methods
- Removed
addUrls
method in favor ofaddUrl
- Fixed bug with robots.txt update
- Fixed bug in addURL method (empty loc)
- Unit tests added for quality assurance
- Updated limits according to sitemaps spec
- Updated search engines urls
- Added change frequency validation