PHP code example of bilaleren / sitemap

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

    

bilaleren / sitemap example snippets


header('Content-Type: application/xml; charset=utf-8');

Map\Entities\Alternate;

$siteMap = new SiteMap([
    new Url('http://example.com/example-1')
]);

// with alternate
$siteMap
    ->registerBasicUrl('http://exampe.com/path-1')
    ->registerAlternate(new Alternate('http://exampe.com/tr/path-1', 'tr'));

$siteMap->registerBasicUrl('http://exampe.com/path-2');

$siteMap->registerUrl(new Url('http://example.com/example-2'));
$siteMap->registerBasicUrl('http://example.com/example-3');

$siteMap
    ->registerBasicUrl('http://example.com/example-4');

// save as sitemap.xml
$siteMap->writeToFile('sitemap.xml');

echo $siteMap;

header('Content-Type: application/xml; charset=utf-8');

Map\Entities\MapIndex;

$siteMap = new SiteMap;

$siteMap->registerBasicUrl('http://example.com/example');
$siteMap->registerBasicMapIndex('http://site.com/sitemap-1.xml');

$siteMapIndex = (new SiteMapIndex)
    ->registerSiteMap($siteMap)
    ->registerMapIndex(new MapIndex('http://site.com/sitemap-2.xml'))
    ->registerMapIndex(new MapIndex('http://site.com/sitemap-3.xml', new DateTime));

// save as sitemap.xml
$siteMapIndex->writeToFile('sitemap.xml');

echo $siteMapIndex;