PHP code example of mjp91 / sitemap-generator

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

    

mjp91 / sitemap-generator example snippets


$items = array(
    new SiteMapItem("http://example.com"),
    new SiteMapItem("http://example.com/foo", "2016-04-16", "monthly", 0.8),
    new SiteMapItem("http://example.com/bar", null, "always", 0.6)
);

$collection = new SiteMapCollection($items);

echo $collection->toXml();

// configure output directory, URL prefix and URLs per site map
$siteMapWriter = new SiteMapXMLWriter('/var/www/example.com', "http://example.com", 50000);

// add records to writer
$siteMapWriter->addItem(new SiteMapItem("http://example.com"));
$siteMapWriter->addItem(new SiteMapItem("http://example.com/foo", "2016-04-16", "monthly", 0.8));
$siteMapWriter->addItem(new SiteMapItem("http://example.com/bar", null, "always", 0.6));
$siteMapWriter->addItem($item);

// finish generation and write index file
$siteMapWriter->writeSiteMapIndex();

// configure output directory, URL prefix and URLs per site map
$siteMapWriter = new SiteMapXMLWriter('/var/www/example.com', "http://example.com", 50000);

// add records to writer
$siteMapWriter->addItem(new SiteMapItem("http://example.com"));

// optional: add alternate links for specific language pages
$item = new SiteMapItem("http://example.com/foo-bar");
$item->addRelAlternate(array(
                               "hreflang" => "en",
                               "href" => "http://example.com/foo-bar"
                           )
                       );
$item->addRelAlternate(array(
                               "hreflang" => "de",
                               "href" => "http://example.com/de/foo-bar"
                           )
                       );
$siteMapWriter->addItem($item);

// finish generation and write index file
$siteMapWriter->writeSiteMapIndex();