bilaleren/sitemap

PHP sitemap.xml 生成器。

v1.0.1 2020-07-07 16:33 UTC

This package is auto-updated.

Last update: 2024-09-08 01:12:27 UTC


README

网站地图是网站管理员通知搜索引擎其网站上可供爬取的页面的一种简单方法。在其最简单的形式中,网站地图是一个XML文件,列出了网站的URL,以及每个URL的附加元数据(最后更新时间、通常更改频率以及相对于网站中其他URL的重要性),以便搜索引擎可以更智能地爬取网站。

安装

composer require bilaleren/sitemap

创建网站地图

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

require 'vendor/autoload.php';

use SiteMap\SiteMap;
use SiteMap\Entities\Url;
use SiteMap\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;

创建网站地图索引

您可以提供多个网站地图文件,但您提供的每个网站地图文件必须不超过50,000个URL,并且大小不能超过50MB(52,428,800字节)。如果您愿意,可以使用gzip压缩您的网站地图文件以减少带宽需求;但是一旦解压,网站地图文件的大小不能超过50MB。如果您要列出的URL超过50,000个,您必须创建多个网站地图文件。

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

require 'vendor/autoload.php';

use SiteMap\SiteMap;
use SiteMap\SiteMapIndex;
use SiteMap\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;