php生成站点地图SiteMap
文件
1. 安装
可以使用下面的composer
包,也可以直接使用文末的类
composer require samdark/sitemap
2. 简单使用
use samdark\sitemap\Sitemap;
use samdark\sitemap\Index;
$sitemap = new Sitemap('./map/sitemap.xml');
// 链接地址、更新时间、更新周期、权重
$sitemap->addItem('https://www.codehui.net', time(), Sitemap::DAILY, 0.8);
$sitemap->addItem('https://www.codehui.net/info', time(), Sitemap::DAILY, 0.5);
// 写入文件
$sitemap->write();
3. 项目中使用
在项目里面一般需要的是批量生成,比如商品详情就需要包含动态参数、首页、关于我们这种不需要参数、有些平台呢单文件上限是10000
个链接,下面分享个我在实战内使用的方法demo。
<?php
use samdark\sitemap\Sitemap;
use samdark\sitemap\Index;
/**
* freq “always”(经常) 、“hourly”(每时)、“daily”(每天)、“weekly”(每周)、“monthly” 月)、“yearly”(每年)
*/
class SiteMapServices
{
/**
* 链接格式
*/
const urlFormat = [
'productDetail' => '/product/%s',
'articleDetail' => '/article/%s',
];
/**
* 链接
* @var array
*/
public $urls = [];
/**
* 网站域名
* @var string
*/
protected $siteUrl;
public function __construct($url)
{
$this->siteUrl = $url;
}
/**
* 网站页面生成链接
* @return void
*/
public function generate()
{
$this->other();
$this->getProductDetail();
return $this;
}
/**
* 获取链接
* @return array|mixed
*/
public function getUrls()
{
return $this->urls;
}
/**
* 设置链接
* @param $data
* @return void
*/
public function setUrls($data = [])
{
$this->urls = $data;
return $this;
}
/**
* 其他页面
* @return void
*/
protected function other()
{
$arrs = ['/home', '/index', '/about'];
foreach ($arrs as $url) {
$url = $this->siteUrl . $url;
$this->urls[] = [
'url' => $url,
'add_time' => time(),
'freq' => 'daily',
'priority' => 1,
];
}
return $this;
}
/**
* 获取商品详情相关url
* @return void
*/
protected function getProductDetail()
{
$productList = [
['id' => 1, 'time' => time()],
['id' => 2, 'time' => time()],
['id' => 3, 'time' => time()],
['id' => 4, 'time' => time()],
['id' => 5, 'time' => time()],
['id' => 6, 'time' => time()],
['id' => 7, 'time' => time()],
['id' => 8, 'time' => time()],
];
foreach ($productList as $item) {
$url = $this->siteUrl . sprintf(self::urlFormat['productDetail'], $item['id']);
$this->urls[] = [
'url' => $url,
'time' => $item['time'],
'freq' => 'monthly',
'priority' => 1,
];
}
return $this;
}
/**
* 创建xml
* @return string
*/
public function createXml($xml_path = '')
{
$xml_path = $xml_path ?: __DIR__ . '/map/sitemap.xml';
$sitemap = new Sitemap($xml_path);
foreach ($this->urls as $item) {
if (is_string($item)) {
$sitemap->addItem($item);
} else {
$sitemap->addItem($item['url'], $item['time'] ?? time(), $item['freq'] ?? Sitemap::DAILY, $item['priority'] ?? 0.8);
}
}
$sitemap->write();
return $xml_path;
}
/**
* 创建分片xml
* @param string $path
* @return void
*/
public function createChunkXml($path = '', $chunk = 10000)
{
$urls = array_chunk($this->urls, $chunk);
foreach ($urls as $key => $list) {
$this->urls = $list;
$this->createXml($path ? $path . "sitemap-{$key}.xml" : '');
}
}
/**
* 链接生成行
* @param $xml_path
* @return mixed|string
*/
public function createLine($xml_path = '')
{
$xml_path = $xml_path ?: public_path() . '/map/sitemap_line.txt';
$sitemap = new Sitemap($xml_path);
foreach ($this->urls as $item) {
file_put_contents($xml_path, $item['url'] . "\r\n", FILE_APPEND);
}
return $xml_path;
}
/**
* 提交到百度
* @param $urls
* @return void
*/
public function submitBaidu($urls = [])
{
$urls = array_merge($urls, $this->urls);
$api = 'http://data.zz.baidu.com/urls?site=https://www.mcwzg.com&token=********************************';
$ch = curl_init();
$options = array(
CURLOPT_URL => $api,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => implode("\n", $urls),
CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
return $this;
}
/**
* 提交到bing
* @param $urls
* @return void
*/
public function submitBing($urls = [])
{
$urls = array_merge($urls, $this->urls);
$api = 'https://www.bing.com/webmaster/api.svc/json/SubmitUrlbatch?apikey=********************************';
$ch = curl_init();
$options = array(
CURLOPT_URL => $api,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => json_encode([
'siteUrl' => 'https://www.mcwzg.com',
'urlList' => $urls
]),
CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
return $this;
}
}
使用方法
$sitemap = new SiteMapServices('https://www.codehui.net);
### 生成一个sitemap文件
$sitemap->generate()->createXml();
### 只生成商品相关页面的sitemap
$sitemap->getProductDetail()->createXml(__DIR__ . '/product_sitemap.xml');
### 生成sitemap文件,每1000个链接为一个文件
$sitemap->generate()->createChunkXml(__DIR__ . '/map/', 1000);
### 设置链接
$sitemap->setUrls([]);
### 获取链接
$sitemap->getProductDetail()->getUrls();
### 生成链接并提交到Baidu站长
$sitemap->generate()->submitBaidu();
### 生成链接并提交到Bing站长
$sitemap->generate()->submitBing();