php生成站点地图SiteMap文件

1. 安装

可以使用下面的composer包,也可以直接使用文末的类

  1. composer require samdark/sitemap

2. 简单使用

  1. use samdark\sitemap\Sitemap;
  2. use samdark\sitemap\Index;
  3. $sitemap = new Sitemap('./map/sitemap.xml');
  4. // 链接地址、更新时间、更新周期、权重
  5. $sitemap->addItem('https://www.codehui.net', time(), Sitemap::DAILY, 0.8);
  6. $sitemap->addItem('https://www.codehui.net/info', time(), Sitemap::DAILY, 0.5);
  7. // 写入文件
  8. $sitemap->write();

3. 项目中使用

在项目里面一般需要的是批量生成,比如商品详情就需要包含动态参数、首页、关于我们这种不需要参数、有些平台呢单文件上限是10000个链接,下面分享个我在实战内使用的方法demo。

  1. <?php
  2. use samdark\sitemap\Sitemap;
  3. use samdark\sitemap\Index;
  4. /**
  5. * freq “always”(经常) 、“hourly”(每时)、“daily”(每天)、“weekly”(每周)、“monthly” 月)、“yearly”(每年)
  6. */
  7. class SiteMapServices
  8. {
  9. /**
  10. * 链接格式
  11. */
  12. const urlFormat = [
  13. 'productDetail' => '/product/%s',
  14. 'articleDetail' => '/article/%s',
  15. ];
  16. /**
  17. * 链接
  18. * @var array
  19. */
  20. public $urls = [];
  21. /**
  22. * 网站域名
  23. * @var string
  24. */
  25. protected $siteUrl;
  26. public function __construct($url)
  27. {
  28. $this->siteUrl = $url;
  29. }
  30. /**
  31. * 网站页面生成链接
  32. * @return void
  33. */
  34. public function generate()
  35. {
  36. $this->other();
  37. $this->getProductDetail();
  38. return $this;
  39. }
  40. /**
  41. * 获取链接
  42. * @return array|mixed
  43. */
  44. public function getUrls()
  45. {
  46. return $this->urls;
  47. }
  48. /**
  49. * 设置链接
  50. * @param $data
  51. * @return void
  52. */
  53. public function setUrls($data = [])
  54. {
  55. $this->urls = $data;
  56. return $this;
  57. }
  58. /**
  59. * 其他页面
  60. * @return void
  61. */
  62. protected function other()
  63. {
  64. $arrs = ['/home', '/index', '/about'];
  65. foreach ($arrs as $url) {
  66. $url = $this->siteUrl . $url;
  67. $this->urls[] = [
  68. 'url' => $url,
  69. 'add_time' => time(),
  70. 'freq' => 'daily',
  71. 'priority' => 1,
  72. ];
  73. }
  74. return $this;
  75. }
  76. /**
  77. * 获取商品详情相关url
  78. * @return void
  79. */
  80. protected function getProductDetail()
  81. {
  82. $productList = [
  83. ['id' => 1, 'time' => time()],
  84. ['id' => 2, 'time' => time()],
  85. ['id' => 3, 'time' => time()],
  86. ['id' => 4, 'time' => time()],
  87. ['id' => 5, 'time' => time()],
  88. ['id' => 6, 'time' => time()],
  89. ['id' => 7, 'time' => time()],
  90. ['id' => 8, 'time' => time()],
  91. ];
  92. foreach ($productList as $item) {
  93. $url = $this->siteUrl . sprintf(self::urlFormat['productDetail'], $item['id']);
  94. $this->urls[] = [
  95. 'url' => $url,
  96. 'time' => $item['time'],
  97. 'freq' => 'monthly',
  98. 'priority' => 1,
  99. ];
  100. }
  101. return $this;
  102. }
  103. /**
  104. * 创建xml
  105. * @return string
  106. */
  107. public function createXml($xml_path = '')
  108. {
  109. $xml_path = $xml_path ?: __DIR__ . '/map/sitemap.xml';
  110. $sitemap = new Sitemap($xml_path);
  111. foreach ($this->urls as $item) {
  112. if (is_string($item)) {
  113. $sitemap->addItem($item);
  114. } else {
  115. $sitemap->addItem($item['url'], $item['time'] ?? time(), $item['freq'] ?? Sitemap::DAILY, $item['priority'] ?? 0.8);
  116. }
  117. }
  118. $sitemap->write();
  119. return $xml_path;
  120. }
  121. /**
  122. * 创建分片xml
  123. * @param string $path
  124. * @return void
  125. */
  126. public function createChunkXml($path = '', $chunk = 10000)
  127. {
  128. $urls = array_chunk($this->urls, $chunk);
  129. foreach ($urls as $key => $list) {
  130. $this->urls = $list;
  131. $this->createXml($path ? $path . "sitemap-{$key}.xml" : '');
  132. }
  133. }
  134. /**
  135. * 链接生成行
  136. * @param $xml_path
  137. * @return mixed|string
  138. */
  139. public function createLine($xml_path = '')
  140. {
  141. $xml_path = $xml_path ?: public_path() . '/map/sitemap_line.txt';
  142. $sitemap = new Sitemap($xml_path);
  143. foreach ($this->urls as $item) {
  144. file_put_contents($xml_path, $item['url'] . "\r\n", FILE_APPEND);
  145. }
  146. return $xml_path;
  147. }
  148. /**
  149. * 提交到百度
  150. * @param $urls
  151. * @return void
  152. */
  153. public function submitBaidu($urls = [])
  154. {
  155. $urls = array_merge($urls, $this->urls);
  156. $api = 'http://data.zz.baidu.com/urls?site=https://www.mcwzg.com&token=********************************';
  157. $ch = curl_init();
  158. $options = array(
  159. CURLOPT_URL => $api,
  160. CURLOPT_POST => true,
  161. CURLOPT_RETURNTRANSFER => true,
  162. CURLOPT_POSTFIELDS => implode("\n", $urls),
  163. CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
  164. );
  165. curl_setopt_array($ch, $options);
  166. $result = curl_exec($ch);
  167. return $this;
  168. }
  169. /**
  170. * 提交到bing
  171. * @param $urls
  172. * @return void
  173. */
  174. public function submitBing($urls = [])
  175. {
  176. $urls = array_merge($urls, $this->urls);
  177. $api = 'https://www.bing.com/webmaster/api.svc/json/SubmitUrlbatch?apikey=********************************';
  178. $ch = curl_init();
  179. $options = array(
  180. CURLOPT_URL => $api,
  181. CURLOPT_POST => true,
  182. CURLOPT_RETURNTRANSFER => true,
  183. CURLOPT_POSTFIELDS => json_encode([
  184. 'siteUrl' => 'https://www.mcwzg.com',
  185. 'urlList' => $urls
  186. ]),
  187. CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
  188. );
  189. curl_setopt_array($ch, $options);
  190. $result = curl_exec($ch);
  191. return $this;
  192. }
  193. }

使用方法

  1. $sitemap = new SiteMapServices('https://www.codehui.net);
  2. ### 生成一个sitemap文件
  3. $sitemap->generate()->createXml();
  4. ### 只生成商品相关页面的sitemap
  5. $sitemap->getProductDetail()->createXml(__DIR__ . '/product_sitemap.xml');
  6. ### 生成sitemap文件,每1000个链接为一个文件
  7. $sitemap->generate()->createChunkXml(__DIR__ . '/map/', 1000);
  8. ### 设置链接
  9. $sitemap->setUrls([]);
  10. ### 获取链接
  11. $sitemap->getProductDetail()->getUrls();
  12. ### 生成链接并提交到Baidu站长
  13. $sitemap->generate()->submitBaidu();
  14. ### 生成链接并提交到Bing站长
  15. $sitemap->generate()->submitBing();