Skip to content

Latest commit

History

8 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Webrium Sitemap

A production-ready PHP library for generating standard XML sitemaps with support for images, videos, hreflang (multi-language), gzip compression, and automatic sitemap splitting for large sites.

Requirements

  • PHP 8.1+
  • ext-xmlwriter
  • ext-zlib

Installation

composer require webrium/sitemap

Basic Usage

useWebrium\Sitemap\Sitemap;
$sitemap = newSitemap('https://example.com');
$sitemap->addUrl('/', changefreq: Sitemap::FREQ_DAILY, priority: 1.0); // → https://example.com$sitemap->addUrl('/about', changefreq: Sitemap::FREQ_MONTHLY, priority: 0.8);
$sitemap->addUrl('/blog', changefreq: Sitemap::FREQ_DAILY, priority: 0.9);
echo$sitemap->generate();

Adding URLs with Full Options

useDateTime;
useWebrium\Sitemap\Sitemap;
$sitemap = newSitemap('https://example.com');
$sitemap->addUrl(
path: '/blog/my-post',
lastmod: newDateTime('2024-06-01'),
changefreq: Sitemap::FREQ_WEEKLY,
priority: 0.7
);

Bulk Adding URLs

$sitemap->addUrls([
['path' => '/page-1', 'priority' => 0.9],
['path' => '/page-2', 'changefreq' => Sitemap::FREQ_DAILY],
['path' => '/page-3', 'lastmod' => newDateTime('2024-01-01')],
]);

Images

$sitemap->addUrl(
path: '/gallery',
images: [
[
'loc' => 'https://example.com/images/photo.jpg',
'title' => 'Photo title',
'caption' => 'Photo caption',
],
]
);

Videos

$sitemap->addUrl(
path: '/videos/intro',
videos: [
[
'thumbnail_loc' => 'https://example.com/thumbs/intro.jpg',
'title' => 'Introduction',
'description' => 'A quick introduction video.',
'duration' => 120,
],
]
);

Hreflang (Multi-language)

$sitemap->addUrl(
path: '/about',
hreflangs: [
['lang' => 'en', 'url' => 'https://example.com/en/about'],
['lang' => 'fa', 'url' => 'https://example.com/fa/about'],
['lang' => 'x-default', 'url' => 'https://example.com/about'],
]
);

Saving to File

// Plain XML$sitemap->saveToFile('/var/www/public/sitemap.xml');
// Gzip compressed (use .gz extension)$sitemap->saveToFile('/var/www/public/sitemap.xml.gz');

Large Sites — Automatic Splitting

When your site has more than 50,000 URLs, use splitAndSave() to automatically split into multiple files and generate a sitemap index:

$sitemap = newSitemap('https://example.com');
// Add all your URLs (any number)foreach ($allPagesas$page) {
$sitemap->addUrl($page->path, $page->updatedAt);
}
$indexXml = $sitemap->splitAndSave(
directory: '/var/www/public/sitemaps',
baseFileUrl: 'https://example.com/sitemaps',
prefix: 'sitemap',
gzip: false
);
// Save the index filefile_put_contents('/var/www/public/sitemap-index.xml', $indexXml);

This generates sitemap-1.xml, sitemap-2.xml, ... and a sitemap index pointing to all of them.

Manual Sitemap Index

$indexXml = $sitemap->generateIndex([
['loc' => 'https://example.com/sitemaps/sitemap-1.xml', 'lastmod' => newDateTime()],
['loc' => 'https://example.com/sitemaps/sitemap-2.xml'],
]);

Change Frequency Constants

ConstantValue
Sitemap::FREQ_ALWAYSalways
Sitemap::FREQ_HOURLYhourly
Sitemap::FREQ_DAILYdaily
Sitemap::FREQ_WEEKLYweekly
Sitemap::FREQ_MONTHLYmonthly
Sitemap::FREQ_YEARLYyearly
Sitemap::FREQ_NEVERnever

Limits

ConstraintValue
Max URLs per file50,000
Max file size50 MB
Duplicate URLsSilently ignored

Running Tests

composer install
./vendor/bin/phpunit tests/

License

MIT

About

A production-ready PHP library for generating XML sitemaps with support for images, videos, hreflang, gzip compression, and automatic splitting for large sites.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages