PHP code example of artemperepechai / sitemap

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

    

artemperepechai / sitemap example snippets


use samdark\sitemap\Sitemap;
use samdark\sitemap\Index;

// create sitemap
$sitemap = new Sitemap(__DIR__ . '/sitemap.xml');

// add some URLs
$sitemap->setLocation('http://example.com/mylink4')
        ->setLastModified(time())
        ->setFrequency(Sitemap::DAILY)
        ->setAlternateLanguage('en', 'http://example.com/en')
        ->setAlternateLanguage('de', 'http://example.com/de')
        ->setAlternateLanguage('fr', 'http://example.com/fr')
        ->setPriority(0.1)
        ->addItem();

// write it
$sitemap->write();

// get URLs of sitemaps written
$sitemapFileUrls = $sitemap->getSitemapUrls('http://example.com/');

// create sitemap for static files
$staticSitemap = new Sitemap(__DIR__ . '/sitemap_static.xml');

// add some URLs
$staticSitemap->setLocation('http://example.com/about')
              ->setLastModified(time())
              ->addItem();
              
$staticSitemap->setLocation('http://example.com/tos')
              ->setLastModified(time())
              ->addItem();
              
$staticSitemap->setLocation('http://example.com/jobs')
              ->setLastModified(time())
              ->addItem();

// write it
$staticSitemap->write();

// get URLs of sitemaps written
$staticSitemapUrls = $staticSitemap->getSitemapUrls('http://example.com/');

// create sitemap index file
$index = new Index(__DIR__ . '/sitemap_index.xml');

// add URLs
foreach ($sitemapFileUrls as $sitemapUrl) {
    $index->addSitemap($sitemapUrl);
}

// add more URLs
foreach ($staticSitemapUrls as $sitemapUrl) {
    $index->addSitemap($sitemapUrl);
}

// write it
$index->write();