ptrufanov1/sitemap-generator

生成 sitemap 文件。支持的文件格式:xml,JSON,csv

v1.0.7 2024-02-12 05:29 UTC

This package is auto-updated.

Last update: 2024-09-12 07:10:04 UTC


README

从 PHP 生成 sitemap 文件。支持的格式:xml,json,csv。

安装

通过 Composer 安装

composer require ptrufanov1/sitemap-generator

确保您的应用程序通过包含 vendor/autoload.php 文件来自动加载 Composer 类。

require "vendor/autoload.php";

要求

本项目模板的最低要求是您的 Web 服务器支持 PHP 8.0。

如何使用

require_once "vendor/autoload.php";

use Ptrufanov1\SitemapGenerator\Sitemap;

$pages = [
	[
		'loc' => 'https://site.ru/',
		'lastmod' => '2022-10-14',
		'priority' => 1,
		'changefreq' => 'hourly',

	],
	[
		'loc' => 'https://site.ru/news',
		'lastmod' => '2022-10-10',
		'priority' => 0.5,
		'changefreq' => 'daily',

	],
	[
		'loc' => 'https://site.ru/about',
		'lastmod' => '2022-10-07',
		'priority' => 0.1,
		'changefreq' => 'weekly',

	],
	[
		'loc' => 'https://site.ru/products',
		'lastmod' => '2022-10-12',
		'priority' => 0.5,
		'changefreq' => 'daily',

	],
	[
		'loc' => 'https://site.ru/products/ps5',
		'lastmod' => '2022-10-11',
		'priority' => 0.1,
		'changefreq' => 'weekly',

	],
	[
		'loc' => 'https://site.ru/products/xbox',
		'lastmod' => '2022-10-12',
		'priority' => 0.1,
		'changefreq' => 'weekly',

	],
	[
		'changefreq' => 'weekly',
		'priority' => 0.3,
		'lastmod' => '2022-10-15',
		'loc' => 'https://site.ru/products/wii',

	]
];

/* Path to save sitemap file */
$path = $_SERVER['DOCUMENT_ROOT']."/sitemap/";

try {
	if (Sitemap::load($pages)->saveXml($path)) {
        echo "Sitemap save successful!";
    }
} catch (Exception $e) {
	echo "Error sitemap generate: ".$e->getMessage();
}

用例

生成 xml sitemap 文件

Sitemap::load($pages)->saveXml($path)

生成 csv sitemap 文件

Sitemap::load($pages)->saveCsv($path)

生成 JSON sitemap 文件

Sitemap::load($pages)->saveJson($path)

如果文件成功生成则返回 true。错误时抛出异常。

MIT License