PHP code example of icamys / php-sitemap-generator

1. Go to this page and download the library: Download icamys/php-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/ */

    

icamys / php-sitemap-generator example snippets




ig = new \Icamys\SitemapGenerator\Config();

// Your site URL.
$config->setBaseURL('https://example.com');

// OPTIONAL. Setting the current working directory to be output directory
// for generated sitemaps (and, if needed, robots.txt)
// The output directory setting is optional and provided for demonstration purposes.
// The generator writes output to the current directory by default. 
$config->setSaveDirectory(sys_get_temp_dir());

// OPTIONAL. Setting a custom sitemap URL base in case if the sitemap files location
// is different from the website root. Most of the time this is unnecessary and can be skipped. 
$config->setSitemapIndexURL('https://example.com/sitemaps/');

$generator = new \Icamys\SitemapGenerator\SitemapGenerator($config);

// Create a compressed sitemap
$generator->enableCompression();

// 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 urls per sitemap
// is the maximum allowed value (see http://www.sitemaps.org/protocol.html)
$generator->setMaxURLsPerSitemap(50000);

// Set the sitemap file name
$generator->setSitemapFileName("sitemap.xml");

// Set the sitemap index file name
$generator->setSitemapIndexFileName("sitemap-index.xml");

// Add alternate languages if needed
$alternates = [
    ['hreflang' => 'de', 'href' => "http://www.example.com/de"],
    ['hreflang' => 'fr', 'href' => "http://www.example.com/fr"],
];

// Add url components: `path`, `lastmodified`, `changefreq`, `priority`, `alternates`
// Instead of storing all urls in the memory, the generator will flush sets of added urls
// to the temporary files created on your disk.
// The file format is 'sm-{index}-{timestamp}.xml'
$generator->addURL('/path/to/page/', new DateTime(), 'always', 0.5, $alternates);

// Optional: add sitemap stylesheet. Note that you need to create
// the file 'sitemap.xsl' beforehand on your own.
$generator->setSitemapStylesheet('sitemap.xsl');

// Flush all stored urls from memory to the disk and close all necessary tags.
$generator->flush();

// Move flushed files to their final location. Compress if the option is enabled.
$generator->finalize();

// Update robots.txt file in output directory or create a new one
$generator->updateRobots();

// Submit your sitemaps to Yandex.
$generator->submitSitemap();



// Initialize the generator
// ...

// Initialize variable with video tags
// For more see the official google documentation:
// https://developers.google.com/search/docs/advanced/sitemaps/video-sitemaps
$videoTags = [
    'thumbnail_loc' => 'http://www.example.com/thumbs/123.jpg',
    'title' => 'Grilling steaks for summer',
    'description' => 'Alkis shows you how to get perfectly done steaks every time',
    'content_loc' => 'http://streamserver.example.com/video123.mp4',
    'player_loc' => 'http://www.example.com/videoplayer.php?video=123',
    'duration' => 600,
    'expiration_date' => '2021-11-05T19:20:30+08:00',
    'rating' => 4.2,
    'view_count' => 12345,
    'publication_date' => '2007-11-05T19:20:30+08:00',
    'family_friendly' => 'yes',
    'restriction' => [
        'relationship' => 'allow',
        'value' => 'IE GB US CA',
    ],
    'platform' => [
        'relationship' => 'allow',
        'value' => 'web mobile',
    ],
    'price' => [
        [
            'currency' => 'EUR',
            'value' => 1.99,
            'type' => 'rent',
            'resolution' => 'hd',
        ]
    ],
    '



// Initialize the generator
// ...

// Initialize a variable with image tags.
// For more see the official google documentation:
// https://developers.google.com/search/docs/advanced/sitemaps/image-sitemaps
$imageTags = [
    'loc' => 'https://www.example.com/thumbs/123.jpg',
    'title' => 'Cat vs Cabbage',
    'caption' => 'A funny picture of a cat eating cabbage',
    'geo_location' => 'Lyon, France',
    'license' => 'https://example.com/image-license',
];

// Alternatively, if you need to pass multiple images per URL, use the format below.
// Maximum number of images per URL is 1000.
$imageTags = [
    [
        'loc' => 'https://www.example.com/thumbs/123.jpg',
        'title' => 'Cat vs Cabbage',
        'caption' => 'A funny picture of a cat eating cabbage',
        'geo_location' => 'Lyon, France',
        'license' => 'https://example.com/image-license',
    ],
    [
        'loc' => 'https://www.example.com/thumbs/456.jpg',
        'title' => 'Dog vs Carrot',
        'caption' => 'A funny picture of a dog eating carrot',
        'geo_location' => 'Lyon, France',
        'license' => 'https://example.com/image-license',
    ]
];

$extensions = [
    'google_image' => $imageTags
];

$generator->addURL('/path/to/page/', null, null, null, null, $extensions);

// generate, flush, etc.
// ...

composer