sujanshresthanet/php-xml-sitemap-generator

PHP XML Sitemap Generator

v2.1.5 2022-06-03 03:10 UTC

This package is auto-updated.

Last update: 2024-09-30 01:41:07 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require Dependents

PHP Sitemap

PHP XML Sitemap Generator.

通过 Composer 安装

将此添加到 composer.json 的 require 块中。

{
    "require": {
        "sujanshresthanet/php-xml-sitemap-generator": "2.*"
    }
}

入门指南

创建 sitemap 对象

use SujanShrestha\Sitemap\Sitemap;

$sitemap = new Sitemap;

向 sitemap 添加项目

$sitemap->addUrlItem($url);
$sitemap->addUrlItem($url);
$sitemap->addUrlItem($url);

您可以添加一些可选参数。

use SujanShrestha\Sitemap\ChangeFreq;

$sitemap->addUrlItem($url, '1.0', ChangeFreq::DAILY, '2015-06-07 10:51:20');
$sitemap->addUrlItem($url, '0.7', ChangeFreq::WEEKLY, new \DateTime('2015-06-03 11:24:20'));

参数是 locprioritychangefreqlastmod。参见此表

参见:http://www.sitemaps.org/protocol.html#xmlTagDefinitions

将其渲染为 XML

echo $sitemap->toString();

// OR

(string) $sitemap;

这是一个示例,说明如何将其作为真实的 sitemap 发送给 Google 或其他搜索引擎

header('Content-Type: application/xml');

echo $sitemap;

exit();

输出

<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
	<url>
		<loc>http://sitemap.io</loc>
	</url>
	<url>
		<loc>http://sitemap.io/foo/bar/?flower=sakura&amp;fly=bird</loc>
		<changefreq>daily</changefreq>
		<priority>1.0</priority>
		<lastmod>2015-06-07T10:51:20+02:00</lastmod>
	</url>
</urlset>

参数

loc

URL 将自动转义。例如,&> 将转换为 &amp;&gt;

如果您想自己转义它,请关闭自动转义

$sitemap->setAutoEscape(false);

参见:http://www.sitemaps.org/protocol.html#escaping

changefreq

有效值

ChangeFreq::ALWAYS;
ChangeFreq::HOURLY;
ChangeFreq::DAILY;
ChangeFreq::WEEKLY;
ChangeFreq::MONTHLY;
ChangeFreq::YEARLY;
ChangeFreq::NEVER;

应使用值 always 来描述每次访问时都更改的文档。

应使用值 never 来描述存档的 URL。

请注意,此标签的值被视为提示,而不是命令。即使搜索引擎爬虫在做出决策时可能考虑此信息,它们也可能比标记为 hourly 的页面更频繁地爬取页面,而比标记为 yearly 的页面更少爬取。

爬虫可能会定期爬取标记为 never 的页面,以便处理那些页面的意外更改。

priority

页面的默认优先级是 0.5。请注意,您分配给页面的优先级不太可能影响您的 URL 在搜索引擎结果页面中的位置。搜索引擎可能在选择同一站点上的 URL 时使用此信息,因此您可以使用此标签增加您的最重要页面出现在搜索索引中的可能性。此外,请注意,将所有 URL 的优先级都设得较高不太可能有所帮助。由于优先级是相对的,它仅用于在您的站点上的 URL 之间进行选择。

lastmod

您的日期格式将自动转换为 W3c Datetime 格式。例如,如果发送的字符串看起来像:2015-06-07 10:51:20,Sitemap 对象将自动将其转换为 2015-06-07T10:51:20+02:00

这是一个示例,说明如何将其作为真实的 sitemap 发送给 Google 或其他搜索引擎

header ("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");
header ("Content-type: text/xml");
header ("Content-Description:XML Sitemap" );

echo $sitemap;

exit();

您可以设置您想要的格式

$sitemap->setDateFormat(\DateTime::ISO8601);

// OR

$sitemap->setDateFormat('Y-m-d');

使用 Sitemap 索引文件(用于组合多个 sitemap 文件)

use SujanShrestha\Sitemap\SitemapIndex;

$index = new SitemapIndex;

$index->addIndexItem('http://domain.com/sitemap1.xml', $lastmod1);
$index->addIndexItem('http://domain.com/sitemap2.xml', $lastmod2);

echo $index->toString();

输出

<?xml version="1.0" encoding="utf-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
	<sitemap>
        <loc>http://domain.com/sitemap1.xml</loc>
        <lastmod>2015-06-07T10:51:20+02:00</lastmod>
    </sitemap>
	<sitemap>
		<loc>http://domain.com/sitemap2.xml</loc>
		<lastmod>2015-06-07T10:51:20+02:00</lastmod>
	</sitemap>
</sitemapindex>

参见:http://www.sitemaps.org/protocol.html#index

更多