gimucco / sitemap
生成 Sitemap 和 Sitemap 索引以优化 SEO
1.2.2
2024-03-21 12:47 UTC
Requires
- php: >=7.4
README
PHP 动态 Sitemap 生成器,支持多 Sitemap 和 Sitemap 索引。通过 hreflang 可选支持多语言
特性
当前特性包括
- 生成单个 Sitemap
- 如果提交超过 500,000 个 URL,则生成多个 Sitemap
- 自动生成 Sitemap 索引
- 指定 URL 是否为移动端友好
- 通过 hreflang 支持多语言
- 自定义名称和路径以保存 XML
- 向 Google 发送 Ping
正在开发中
- 图像支持
- 视频支持
要求
PHP >= 7.4
安装
通过 Composer 安装
composer require gimucco/sitemap
生成简单 Sitemap 的代码示例
以下代码演示了如何从 URL 数组开始生成简单的 Sitemap。有关更多详细示例和选项,请参阅 examples
文件夹。
use Gimucco\Sitemap\Runner; use Gimucco\Sitemap\Url; require_once __DIR__.'/../vendor/autoload.php'; // This is the Path where the xml files will be saved. $sitemaps_folder_path = __DIR__.'/sitemaps/'; // This is the URL corresponding to the path above $sitemaps_folder_url = 'https://yourdomain.com/sitemaps/'; // Array containing your URLs $urls = ['https://yourdomain.com/', 'https://yourdomain.com/contacts', 'https://yourdomain.com/signup', 'https://yourdomain.com/login']; // Starting the Runner // First parameter is the local path to the folder where Sitemaps will be saved // Second parameter is the URL to reach the sitemaps // Third parameter are options, E.g. Verbosity $_Runner = new Runner($sitemaps_folder_path, $sitemaps_folder_url, [Runner::OPTION_VERBOSE]); foreach ($urls as $url) { // Push the URL to the sitemap // First parameter is the URL of the resource you want to add to the sitemap // Second parameter is the Date of last update in ISO8601 format // Third parameter is the priority (0.1 to 1) // Fourth parameter is the update frequency (e.g. daily) $_Runner->pushURL($url, Url::timeNow(), Url::PRIORITY_HIGHEST, Url::FREQ_ALWAYS); } // Write Sitemaps and cleanup $_Runner->end(); // Optional, send Ping to Google to refresh the Sitemap $_Runner->pingGoogle();
工具:频率
Url 类在公共常量中包含默认优先级值。
Sitemap::FREQ_ALWAYS; // always Sitemap::FREQ_HOURLY; // hourly Sitemap::FREQ_DAILY; // daily Sitemap::FREQ_WEEKLY; // weekly Sitemap::FREQ_MONTHLY; // monthly Sitemap::FREQ_YEARLY; // yearly Sitemap::FREQ_NEVER ; // never
工具:优先级
Url 类在公共常量中包含默认优先级值。
Sitemap::PRIORITY_HIGHEST; //1; Sitemap::PRIORITY_HIGHER; //0.9; Sitemap::PRIORITY_HIGH; //0.7; Sitemap::PRIORITY_AVERAGE; //0.5; Sitemap::PRIORITY_LOW; //0.3; Sitemap::PRIORITY_LOWER; //0.2; Sitemap::PRIORITY_LOWEST; //0.1;
工具:最后更新
Url 类包含公共静态方法,用于将日期和时间戳转换为 Sitemap 正确格式的 ISO8601。
Url::convertDateToISO8601("2022-03-13 12:34:56"); // Output: 2022-03-13T12:34:56+00:00 Url::convertTimestampToISO8601(1647147559); // Output: 2022-03-13T04:59:19+00:00
此外,Url 类还包含标准时间(已格式化为 ISO8601)的公共静态方法。
Sitemap::timeNow(); // Now Sitemap::timeYesterday(); // 24 hours ago Sitemap::timeOneWeek(); // One week ago { Sitemap::timeOneMonth(); // One month ago
示例
有关如何使用各种选项的快速示例,请参阅 examples
文件夹。
可用示例
- 小型简单 Sitemap
- 大型随机 Sitemap
- 具有多个语言 URL 的复杂 Sitemap
- 重命名 Sitemap