定制 asika/sitemap 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

asika/sitemap

Composer 安装命令:

composer require asika/sitemap

包简介

PHP Simple Sitemap Generator

README 文档

README

GitHub Actions Workflow Status Packagist Version Packagist Downloads

PHP Simple Sitemap Generator. Follows the W3C Sitemap Protocol

Installation via Composer

composer require asika/sitemap

Getting Started

Create a sitemap object:

use Asika\Sitemap\Sitemap;

$sitemap = new Sitemap();

Add items to sitemap:

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

You can add some optional params.

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'));

The arguments are loc, priority, changefreq and lastmod. See this table:

Params Required Description
loc required URL of the page. This URL must begin with the protocol (such as http) and end with a trailing slash, if your web server requires it. This value must be less than 2,048 characters.
priority optional The priority of this URL relative to other URLs on your site. Valid values range from 0.0 to 1.0. This value does not affect how your pages are compared to pages on other sites—it only lets the search engines know which pages you deem most important for the crawlers.
changefreq optional How frequently the page is likely to change. This value provides general information to search engines and may not correlate exactly to how often they crawl the page.
lastmod optional The date of last modification of the file. This date should be in W3C Datetime format. This format allows you to omit the time portion, if desired, and use YYYY-MM-DD.

See: http://www.sitemaps.org/protocol.html#xmlTagDefinitions

Render it to XML:

echo $sitemap->render();

// OR

(string) $sitemap;

Save to file:

$sitemap->save('/path/to/sitemap.xml');

// OR

$file = new SplFileInfo('/path/to/sitemap.xml');

$sitemap->save($file);

This is an example to send it as real sitemap for Google or other search engine:

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

echo $sitemap;

exit();

Use output() to instantly print header and XML body:

$sitemap->output();

exit();

Handle Psr7 Response

$response = new Response();

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

return $response;

The XML output in browser:

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

Arguments

loc

The URL will be auto escaped. For example, the &, > will convert to &amp;, &gt;.

If you want to escape it yourself, set auto escape off:

$sitemap->setAutoEscape(false);

See: http://www.sitemaps.org/protocol.html#escaping

changefreq

Valid values are:

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

The value always should be used to describe documents that change each time they are accessed.

The value never should be used to describe archived URLs.

Please note that the value of this tag is considered a hint and not a command. Even though search engine crawlers may consider this information when making decisions, they may crawl pages marked hourly less frequently than that, and they may crawl pages marked yearly more frequently than that.

Crawlers may periodically crawl pages marked never so that they can handle unexpected changes to those pages.

priority

The default priority of a page is 0.5. Please note that the priority you assign to a page is not likely to influence the position of your URLs in a search engine's result pages. Search engines may use this information when selecting between URLs on the same site, so you can use this tag to increase the likelihood that your most important pages are present in a search index. Also, please note that assigning a high priority to all of the URLs on your site is not likely to help you. Since the priority is relative, it is only used to select between URLs on your site.

lastmod

Your date format will auto convert to W3c Datetime format. for example, if you send a string look like: 2015-06-07 10:51:20, Sitemap object will auto convert it to 2015-06-07T10:51:20+02:00.

You can set the format you want:

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

// OR

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

Google News Sitemap

Please see Google News Sitemap document.

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

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

The format:

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

Using Sitemap index files (to group multiple sitemap files)

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();

Output:

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

See: http://www.sitemaps.org/protocol.html#index

More

asika/sitemap 适用场景与选型建议

asika/sitemap 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19.28k 次下载、GitHub Stars 达 19, 最近一次更新时间为 2015 年 06 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「Sitemap」 「google」 「seo」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 asika/sitemap 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 asika/sitemap 我们能提供哪些服务?
定制开发 / 二次开发

基于 asika/sitemap 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 19.28k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 22
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 19
  • Watchers: 1
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-06-07