asika/sitemap

PHP 简单网站地图生成器

2.1.0 2024-08-08 13:33 UTC

This package is auto-updated.

Last update: 2024-09-08 13:44:15 UTC


README

PHP 简单网站地图生成器。遵循 W3C 网站地图协议

通过 Composer 安装

composer require asika/sitemap

入门

创建网站地图对象

use Asika\Sitemap\Sitemap;

$sitemap = new Sitemap();

向网站地图添加项目

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

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

use Asika\Sitemap\ChangeFreq;

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

参数是 locprioritychangefreqlastmod。请参阅此表

查看: http://www.sitemaps.org/protocol.html#xmlTagDefinitions

将其渲染为 XML

echo $sitemap->render();

// OR

(string) $sitemap;

这是一个示例,用于将其作为真实网站地图发送给 Google 或其他搜索引擎

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

echo $sitemap;

exit();

使用 output() 立即打印头部和 XML 主体

$sitemap->output();

exit();

处理 Psr7 响应

$response = new Response();

$response = $sitemap->handleResponse($response);

return $response;

在浏览器中的 XML 输出

<?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 日期时间格式。例如,如果您发送的字符串看起来像:2015-06-07 10:51:20,则网站地图对象将自动将其转换为 2015-06-07T10:51:20+02:00

您可以设置所需的格式

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

// OR

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

Google 新闻网站地图

请参阅 Google 新闻网站地图 文档。

$sitemap = new \Asika\Sitemap\NewsSitemap();

$sitemap->addItem(
    $url,
    $newsTitle,
    'Publication Name',
    'en-us',
    $publishedDate
);

格式

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
    xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">
  <url>
  <loc>http://www.example.org/business/article55.html</loc>
  <news:news>
    <news:publication>
      <news:name>The Example Times</news:name>
      <news:language>en</news:language>
    </news:publication>
    <news:publication_date>2008-12-23</news:publication_date>
    <news:title>Companies A, B in Merger Talks</news:title>
  </news:news>
  </url>
</urlset>

使用网站地图索引文件(用于将多个网站地图文件分组)

use Asika\Sitemap\SitemapIndex;

$index = new SitemapIndex();

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

echo $index->render();

输出

<?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

更多