Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/muhammetsafak/sitemapgenerator

Basic Sitemap.XML Generator
https://github.com/muhammetsafak/sitemapgenerator

Last synced: 26 days ago
JSON representation

Basic Sitemap.XML Generator

Awesome Lists containing this project

README

        

# Sitemap Generator

This class uses simple XML syntax. It is prepared simply to create a sitemap. Supports creating sitemaps for Images, Videos and News...

It has been prepared using the document provided by Google to implement current standards ([https://developers.google.com/search/docs/advanced/sitemaps/overview](https://developers.google.com/search/docs/advanced/sitemaps/overview)).

## Requirements

- PHP 7.2 or higher
- PHP SimpleXML Extension
- PHP DOM Extension

## Installation

```
composer require muhammetsafak/sitemap-generator
```

## Usage

_**Note :** If you want to get the XML output as a string instead of writing it directly to a file; You can use the `getContent()` method._

_**Note :** If you want the generated XML output to be formatted, you can use the `setFormatOutput()` method._

### Standard Sitemap Generator

```php
require_once "vendor/autoload.php";
use \MuhammetSafak\SitemapGenerator\Generator;

$generator = new Generator();
$generator->setBaseURL('https://example.com/');

for ($i = 1; $i <= 3; ++$i) {
$path = "/path/page/" . $i;
$generator->addUrl($path, new DateTime(), [
'changefreq' => 'weekly',
'priority' => '0.6'
]);
}

$generator->save(__DIR__ . '/sitemap.xml', true);
$generator->clear();
```

The example above produces the following output;

```xml


https://example.com/path/page/1
2022-04-26T19:07:09+00:00
weekly
0.6


https://example.com/path/page/2
2022-04-26T19:07:09+00:00
weekly
0.6


https://example.com/path/page/3
2022-04-26T19:07:09+00:00
weekly
0.6

```

Review the following example on identifying alternatives.

```php
require_once "vendor/autoload.php";
use \MuhammetSafak\SitemapGenerator\Generator;

$generator = new Generator();
$generator->setBaseURL('https://example.com/');

$generator->addAlternate('fr', 'https://example.com/fr/');
$generator->addAlternate('de', 'https://example.com/de/')

for ($i = 1; $i <= 3; ++$i) {
$path = "/path/page/" . $i;
$generator->addUrl($path, new DateTime(), [
'changefreq' => 'weekly',
'priority' => '0.6'
]);
}

$generator->save(__DIR__ . '/sitemap.xml', true);
$generator->clear();
```

The example above produces the following output;

```xml


https://example.com/path/page/1
2022-04-26T19:07:09+00:00
weekly
0.6




https://example.com/path/page/2
2022-04-26T19:07:09+00:00
weekly
0.6




https://example.com/path/page/3
2022-04-26T19:07:09+00:00
weekly
0.6


```

### Video Sitemap Generator

```php
require_once "vendor/autoload.php";
use \MuhammetSafak\SitemapGenerator\Generator;

$generator = new Generator(Generator::NEWS);
$generator->setBaseURL('https://example.com/');

$video = [
'thumbnail' => 'https://example.com/thumbs/1.jpg',
'title' => 'Video Title 1',
'description' => 'Video Description Value',
'content_loc' => 'https://example.com/videos/1.mp4',
'player_loc' => 'https://example.com/videoplayer.php?video=1',
'duration' => 600,
'expiration_date' => '2021-11-05T19:20:30+08:00', // or DateTimeInterface object
'rating' => '4.2',
'view_count' => 12345,
'publication_date' => '2012-11-05T19:20:30+08:00', // or DateTimeInterface object
'family_friendly' => true, // [true|false|"yes"|"no"]
'platform' => [
'relationship' => 'allow', // ["allow"|"deny"]
'value' => 'web mobil tv' // "web" "mobil" "tv"
],
'restriction' => [
'relationship' => 'allow', // ["allow"|"deny"]
'value' => 'IE GB US CA'
],
'price' => [
'currency' => 'EUR',
'value' => '1.99'
],
'requires_subscription' => true, // [true|false|"yes"|"no"]
'uploader' => [
'info' => 'https://example.com/user/admin',
'value' => 'Admin'
],
'live' => false, // [true|false|"yes"|"no"]
];

$generator->addUrl('/path/video/1', new DateTime(), $video);

$generator->save(__DIR__ . '/sitemap.xml', true);
$generator->clear();
```

The example above produces the following output;

```xml


https://example.com/path/video/1

https://example.com/thumbs/1.jpg
Video Title 1
Video Description Value
https://example.com/videos/1.mp4
https://example.com/videoplayer.php?video=1
600
2021-11-05T19:20:30+08:00
4.2
12345
2007-11-05T19:20:30+08:00
yes
web mobil tv
IE GB US CA
1.99
yes
Admin

no

```

### Image Sitemap Generator

```php
require_once "vendor/autoload.php";
use \MuhammetSafak\SitemapGenerator\Generator;

$generator = new Generator(Generator::NEWS);
$generator->setBaseURL('https://example.com/');

$generator->addUrl('/path/page/1', new DateTime(), [
'image' => 'https://example.com/files/image1.jpg'
]);

$generator->addUrl('/path/page/2', new DateTime(), [
'image' => [
'https://example.com/files/image2.jpg',
'https://example.com/files/image3.jpg',
'https://example.com/files/image4.jpg'
]
]);

$generator->save(__DIR__ . '/sitemap.xml', true);
$generator->clear();
```

The example above produces the following output;

```xml


https://example.com/path/page/1

https://example.com/files/image1.jpg



https://example.com/path/page/2

https://example.com/files/image2.jpg


https://example.com/files/image3.jpg


https://example.com/files/image4.jpg

```

### News Sitemap Generator

```php
require_once "vendor/autoload.php";
use \MuhammetSafak\SitemapGenerator\Generator;

$generator = new Generator(Generator::NEWS);
$generator->setBaseURL('https://example.com/');

for ($i = 1; $i <= 3; ++$i) {
$path = "/path/news/" . $i;
$generator->addUrl($path, new DateTime(), [
'publication' => [
'name' => 'The Example Times',
'language' => 'en'
],
'title' => 'Headline Of Breaking News #' . $i,
]);
}

$generator->save(__DIR__ . '/sitemap.xml', true);
$generator->clear();
```

The example above produces the following output;

```xml


https://example.com/path/news/1


The Example Times
en

2022-06-22
Headline Of Breaking News #1



https://example.com/path/news/2


The Example Times
en

2022-06-22
Headline Of Breaking News #2



https://example.com/path/news/3


The Example Times
en

2022-06-22
Headline Of Breaking News #3

```

## Credits

- [Muhammet ŞAFAK](https://www.muhammetsafak.com.tr) <>

## License

Copyright © 2022 [MIT License](./LICENSE)