SiteMapGenerator is a PHP library that makes it easy to generate a SiteMap and SiteMapIndex (including Google specific tags).
There are 2 ways to install SiteMapGenerator.
If you use Composer, SiteMapGenerator is available by Packagist.
Just add the dependencies to your composer.json:
{
"require": {
"cyberomulus/sitemap-generator": "~2.0"
}
}
Else, got to the page github of SiteMapGenerator and choose the release of your choice.
You can download the source code with the link 'Download ZIP'.
Place the directory in the ZIP in a lib folder (for example) of your project.
It takes minimum PHP version 5.2.0 with XMLWriter extension enabled (it is enabled by default).
- Create one or more
SiteMapEntry
- Create one
SiteMapIndex
- Add all
SiteMapEntry
inSiteMapIndex
- Create a Formatter (example :
XMLFormatter
) - Use
Formatter::formatSiteMapIndex()
- Create one or more
URLEntry
- Create one
SiteMap
- Add all
URLEntry
inSiteMap
- Create a Formatter (example :
XMLFormatter
) - Use
Formatter::formatSiteMap()
- Create one or more
GoogleImageEntry
- Add all
GoogleImageEntry
inURLEntry
Juste set Parameter at false in constructor of SiteMap
.
All GoogleImageEntry are not displayed
Create a class extends Formatter
.
<?php
require 'lib/SiteMapGenerator/src/SiteMap.php';
require 'lib/SiteMapGenerator/src/entries/SiteMapEntry.php';
require 'lib/SiteMapGenerator/src/entries/URLEntry.php';
require 'lib/SiteMapGenerator/src/entries/GoogleImageEntry.php';
require 'lib/SiteMapGenerator/src/formatter/Formatter.php';
require 'lib/SiteMapGenerator/src/formatter/XMLFormatter.php';
use Cyberomulus\SiteMapGenerator\SiteMap;
use Cyberomulus\SiteMapGenerator\Entries\URLEntry;
use Cyberomulus\SiteMapGenerator\Entries\GoogleImageEntry;
use Cyberomulus\SiteMapGenerator\Formatter\XMLFormatter;
// create a sitemap
$sitemap = new SiteMap(true);
// create url
$url1 = new URLEntry("http://www.test.com/ok.php", new DateTime(), URLEntry::CHANGE_FEQUENCE_DAILY, "0.5");
$url2 = new URLEntry("http://www.test.com/nice.php", new DateTime(), URLEntry::CHANGE_FEQUENCE_NEVER);
$url3 = new URLEntry("http://www.test.com/nice.php?test=ok&restet=super", new DateTime(), URLEntry::CHANGE_FEQUENCE_NEVER);
// create a image for Google's extra
$image1 = new GoogleImageEntry("http://www.test.com/image/img1.jpg",
"a title for image",
"a caption for image",
"Brussels, Belgium",
"http://www.test.com/image/license.txt");
$image2 = new GoogleImageEntry("http://www.test.com/image/img1.jpg",
"a another title for image",
"a another caption for image",
null,
"http://www.test.com/image/license.txt");
// add image in url
$url1->addGoogleImageEntry($image1);
$url1->addGoogleImageEntry($image2);
// add url in sitemap
$sitemap->addUrlEntry($url1);
$sitemap->addUrlEntry($url2);
$sitemap->addUrlEntry($url3);
// create formatter
$formatter = new XMLFormatter();
// output sitemap
echo "<pre>" . $formatter->formatSiteMap($sitemap) . "</pre>";
The result :
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://www.test.com/ok.php</loc>
<lastmod>2015-05-17T15:04:38+02:00</lastmod>
<changefreq>daily</changefreq>
<priority>0.5</priority>
<image:image>
<image:loc>http://www.test.com/image/img1.jpg</image:loc>
<image:title>a title for image</image:title>
<image:caption>a caption for image</image:caption>
<image:geo_location>Brussels, Belgium</image:geo_location>
<image:license>http://www.test.com/image/license.txt</image:license>
</image:image>
<image:image>
<image:loc>http://www.test.com/image/img1.jpg</image:loc>
<image:title>a another title for image</image:title>
<image:caption>a another caption for image</image:caption>
<image:license>http://www.test.com/image/license.txt</image:license>
</image:image>
</url>
<url>
<loc>http://www.test.com/nice.php</loc>
<lastmod>2015-05-17T15:04:38+02:00</lastmod>
<changefreq>never</changefreq>
</url>
<url>
<loc>http://www.test.com/nice.php?test=ok&amp;restet=super</loc>
<lastmod>2015-05-17T15:04:38+02:00</lastmod>
<changefreq>never</changefreq>
</url>
</urlset>
<?php
require 'lib/SiteMapGenerator/src/SiteMapIndex.php';
require 'lib/SiteMapGenerator/src/entries/SiteMapEntry.php';
require 'lib/SiteMapGenerator/src/formatter/Formatter.php';
require 'lib/SiteMapGenerator/src/formatter/XMLFormatter.php';
use Cyberomulus\SiteMapGenerator\SiteMapIndex;
use Cyberomulus\SiteMapGenerator\Entries\SiteMapLEntry;
use Cyberomulus\SiteMapGenerator\Formatter\XMLFormatter;
// create sitemapindex
$sitemapindex = new SiteMapIndex();
// create sitemap entries
$sitemap1 = new SiteMapLEntry("http://www.test.com/sitemap1.xml", new DateTime());
$sitemap2 = new SiteMapLEntry("http://www.test.com/sitemap1.xml", new DateTime());
$sitemap3 = new SiteMapLEntry("http://www.test.com/sitemap.php?code=3&restet=super", new DateTime());
// add sitemap entries in sitemapindex
$sitemapindex->addSiteMapEntry($sitemap1);
$sitemapindex->addSiteMapEntry($sitemap2);
$sitemapindex->addSiteMapEntry($sitemap3);
// create formatter
$formatter = new XMLFormatter();
// output sitemapindex
echo "<pre>" . $formatter->formatSiteMapIndex($sitemapindex) . "</pre>";
The result :
<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>http://www.test.com/sitemap1.xml</loc>
<lastmod>2015-05-17T15:07:40+02:00</lastmod>
</sitemap>
<sitemap>
<loc>http://www.test.com/sitemap1.xml</loc>
<lastmod>2015-05-17T15:07:40+02:00</lastmod>
</sitemap>
<sitemap>
<loc>http://www.test.com/sitemap.php?code=3&amp;restet=super</loc>
<lastmod>2015-05-17T15:07:40+02:00</lastmod>
</sitemap>
</sitemapindex>
SiteMapGenerator is under MIT license (license free).
You will find the license text in the file LICENSE.