PHP code example of cyberomulus / sitemap-generator

1. Go to this page and download the library: Download cyberomulus/sitemap-generator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cyberomulus / sitemap-generator example snippets


		
	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"

		
	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"