A simple yet powerful PHP library for generating XML sitemaps compliant with the Sitemaps.org protocol. This library helps you create sitemaps for search engine optimization (SEO) and can handle large websites with multiple sitemap files and sitemap index files.
- Generate XML sitemaps according to sitemaps.org protocol
- Support for multiple sitemaps with automatic sitemap index generation
- GZip compression support for smaller file sizes
- Automatic robots.txt generation and updating
- Submit sitemaps to search engines (Google, Bing)
- Memory-efficient using SplFixedArray
- Validates URLs, priorities, and change frequencies
- PSR-4 autoloading compatible
- Well-documented and tested
- PHP >= 5.5.0
- ext-SPL (Standard PHP Library)
- ext-curl (for sitemap submission to search engines)
- ext-zlib (optional, for GZip compression)
- ext-mbstring (optional, for accurate URL length validation)
composer require icamys/php-sitemap-generator- Download or clone this repository
- Include the autoloader in your project:
require_once 'path/to/php-sitemap-generator/vendor/autoload.php';<?php
require_once 'vendor/autoload.php';
use Icamys\SitemapGenerator\SitemapGenerator;
// Create a new sitemap generator
$generator = new SitemapGenerator('https://example.com');
// Add URLs
$generator->addUrl('/', new DateTime(), 'daily', '1.0');
$generator->addUrl('/about', new DateTime(), 'monthly', '0.8');
$generator->addUrl('/contact', new DateTime(), 'monthly', '0.8');
// Generate the sitemap
$generator->createSitemap();
// Write to file
$generator->writeSitemap();
// Update robots.txt
$generator->updateRobots();
echo "Sitemap generated successfully!";use Icamys\SitemapGenerator\SitemapGenerator;
// Basic initialization
$generator = new SitemapGenerator('https://example.com');
// With custom output directory
$generator = new SitemapGenerator('https://example.com', './sitemaps/');// Add URL with all parameters
$generator->addUrl(
'/products/item-1', // URL path
new DateTime('2024-01-15'), // Last modified date
'weekly', // Change frequency
'0.9' // Priority (0.0 to 1.0)
);
// Add URL with minimal parameters
$generator->addUrl('/simple-page');$urls = [
['/', new DateTime(), 'daily', '1.0'],
['/about', new DateTime(), 'monthly', '0.8'],
['/contact', new DateTime(), 'yearly', '0.5'],
];
$generator->addUrls($urls);Valid values for the changefreq parameter:
always- Document changes each time it is accessedhourly- Updated every hourdaily- Updated every dayweekly- Updated every weekmonthly- Updated every monthyearly- Updated every yearnever- Archived URL, never changes
The priority parameter accepts values from 0.0 to 1.0:
1.0- Highest priority0.5- Medium priority (default)0.0- Lowest priority
$generator = new SitemapGenerator('https://example.com', './output/');
// Enable GZip compression
$generator->createGZipFile = true;
// Set custom filenames
$generator->sitemapFileName = 'my-sitemap.xml';
$generator->sitemapIndexFileName = 'my-sitemap-index.xml';
$generator->robotsFileName = 'robots.txt';
// Set custom limits
$generator->maxURLsPerSitemap = 10000; // Default: 50000
$generator->maxSitemaps = 10000; // Default: 50000$generator = new SitemapGenerator('https://example.com');
$generator->maxURLsPerSitemap = 10000; // Split into multiple files
// Add thousands of URLs
for ($i = 1; $i <= 50000; $i++) {
$generator->addUrl("/page-{$i}", new DateTime(), 'weekly', '0.7');
}
// This will create multiple sitemap files and a sitemap index
$generator->createSitemap();
$generator->writeSitemap();$generator->createSitemap();
// Get sitemaps as array
$sitemaps = $generator->toArray();
foreach ($sitemaps as $sitemap) {
$filename = $sitemap[0];
$xmlContent = $sitemap[1];
// Process or store as needed
echo "Sitemap: {$filename}\n";
}$generator->createSitemap();
$generator->writeSitemap();
try {
$results = $generator->submitSitemap();
foreach ($results as $result) {
echo "Submitted to {$result['site']}: ";
echo "HTTP {$result['http_code']} - {$result['message']}\n";
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}// Get total number of URLs
$count = $generator->countUrls();
echo "Total URLs: {$count}\n";
// Get all URLs as array
$urls = $generator->getUrls();
foreach ($urls as $url) {
print_r($url);
}__construct(string $baseURL, string $basePath = "")$baseURL- Your website URL (with trailing slash)$basePath- Directory path where sitemaps will be stored
addUrl(string $url, DateTime $lastModified = null,
string $changeFrequency = null, string $priority = null)Add a single URL to the sitemap.
addUrls(array $urlsArray)Add multiple URLs at once.
createSitemap()Generate the sitemap(s) in memory.
writeSitemap()Write sitemap files to disk.
updateRobots()Update or create robots.txt file with sitemap location.
submitSitemap(): arraySubmit sitemap to search engines (Google, Bing). Returns array of results.
toArray(): arrayGet sitemaps as array without writing to files.
getUrls(): arrayGet all URLs with their parameters as array.
countUrls(): intGet the total number of URLs.
getVersion(): stringGet the library version.
Complete examples are available in the examples directory:
- basic-usage.php - Basic sitemap generation
- advanced-usage.php - Advanced features and options
For detailed documentation, see the docs directory:
This library follows the Sitemaps.org Protocol:
- Maximum 50,000 URLs per sitemap file
- Maximum 10MB per sitemap file (uncompressed)
- Maximum 50,000 sitemaps per sitemap index
- URL length maximum 2,048 characters
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
# Run tests (when available)
composer test
# Run linting
composer lintSee CHANGELOG.md for version history and changes.
This project is licensed under the MIT License - see the LICENSE file for details.
- Original Author: Prisacari Dmitrii
- Contributors: See CONTRIBUTORS.md
- Report bugs: GitHub Issues
- Ask questions: GitHub Discussions
Made with ❤️ by the PHP community