网站sitemap生成

1、xml文件可以使用工具生成,比如:sitemapXML在线生成

2、HTML生成的话可以参考以下代码,先使用 1、生成sitemap.xml文件并放至网站根目录,然后在网站根目录创建sitemap.php 将以下代码放入执行,就会生成sitemap.html

复制代码
<?php

function getPageTitleByCurl($url) {
    // 过滤URL中的异常字符(解决文档中URL含特殊符号的问题)
    $url = trim($url, '\'"<> ');
    
    // 初始化curl
    $ch = curl_init();
    // 设置curl参数
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // 跟随跳转
    curl_setopt($ch, CURLOPT_TIMEOUT, 10); // 超时时间10秒
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 关闭SSL验证(避免https链接报错)
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    
    // 执行curl并获取响应内容
    $response = curl_exec($ch);
    
    // 捕获curl错误
    $curlError = curl_errno($ch);
    curl_close($ch);
    
    // 解析页面title(兼容不同HTML格式)
    preg_match('/<title>(.*?)<\/title>/is', $response, $matches);
    if (!empty($matches[1])) {
        $title = trim($matches[1]);
        return ['title' => $title, 'error' => ''];
    } else {
        return ['title' => '无标题页面', 'error' => '网页解析失败,未找到title标签'];
    }
}

/**
 * 从sitemap.xml生成sitemap.html文件
 * 适配网站:www.71huyu.com
 * 处理异常:xml解析失败、链接失效、网页解析失败等场景
 */

// 1. 定义文件路径(sitemap.xml路径、生成的sitemap.html路径)
$xmlPath = $_SERVER['DOCUMENT_ROOT'] . '/sitemap.xml'; // 网站根目录下的sitemap.xml
$htmlPath = $_SERVER['DOCUMENT_ROOT'] . '/sitemap.html'; // 生成的sitemap.html保存路径

// 2. 检查sitemap.xml是否存在
if (!file_exists($xmlPath)) {
    die("错误:sitemap.xml文件不存在,请先确保sitemap.xml在网站根目录下");
}

// 3. 读取并解析sitemap.xml
$xmlContent = file_get_contents($xmlPath);
if ($xmlContent === false) {
    die("错误:无法读取sitemap.xml文件,请检查文件权限");
}

// 解析XML(禁止报错,手动处理解析失败场景)
libxml_use_internal_errors(true);
$xml = simplexml_load_string($xmlContent);
libxml_clear_errors();

if (!$xml) {
    die("错误:sitemap.xml解析失败,可能是文件格式错误或不支持的XML类型");
}

// 4. 构建sitemap.html的HTML结构
$htmlContent = '<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>网站地图</title>
<style>
*{margin:0;padding:0;box-sizing:border-box}
body{font-family:Microsoft YaHei, Arial, sans-serif;line-height:1.8;background:#f7f8fa;padding:20px}
.container{max-width:1000px;margin:0 auto;background:#fff;padding:30px;border-radius:8px;box-shadow:0 2px 10px rgba(0,0,0,.05)}
h1{font-size:24px;margin-bottom:20px;color:#333;border-bottom:1px solid #eee;padding-bottom:10px}
h2{font-size:18px;margin:20px 0 10px;color:#444}
ul{list-style:none}
li{margin:8px 0}
a{color:#1677ff;text-decoration:none}
a:hover{text-decoration:underline}
.error-note{color:#f5222d;font-size:12px;margin-left:10px}
</style>
</head>
<body>
<div class="container">
<h1>网站地图</h1>

<h2>站点页面</h2>
<ul>';

// 解析XML中的url(兼容标准sitemap.xml格式,urlset > url > loc)
if (isset($xml->url)) {
    foreach ($xml->url as $url) {
        $loc = $url->loc; // 获取链接地址
        
        $title = getPageTitleByCurl($loc); // 若XML中无title,默认用链接作为标题
        
        // 拼接列表项
        $htmlContent .= '<li><a href="' . htmlspecialchars($loc) . '" target="_blank">' . htmlspecialchars($title['title']) . '</a></li>';
    }
} else {
    $htmlContent .= '<li>未从sitemap.xml中解析到任何链接</li>';
}

// 6. 完成HTML结构拼接
$htmlContent .= '</ul>

<p style="margin-top:30px;color:#999;font-size:12px"></p>
</div>
</body>
</html>';

// 7. 生成sitemap.html文件
$writeResult = file_put_contents($htmlPath, $htmlContent);
if ($writeResult === false) {
    die("错误:无法生成sitemap.html文件,请检查网站根目录写入权限");
}

echo "成功!sitemap.html已生成,路径:" . $htmlPath;
?>
相关推荐
剑神一笑3 小时前
Linux lsof 命令深度解析:从文件描述符到进程追踪
linux·运维·php
BingoGo3 小时前
免费可商用 PHP 管理后台 CatchAdmin V5.3.1 发布 后台打包直降 5s 内
后端·php
JaguarJack4 小时前
免费可商用 PHP 管理后台 CatchAdmin V5.3.1 发布 后台打包直降 5s 内
后端·php·laravel
ELI_He9994 小时前
Laravel Sail
php·laravel
傻啦嘿哟7 小时前
解决DNS污染:防止OpenClaw解析API域名到虚假地址
开发语言·php
dualven_in_csdn8 小时前
cmd切换到powershell (一)
服务器·开发语言·php
Cheng小攸8 小时前
实验九:防火墙安全认证和审计实验
开发语言·安全·php
会Tk矩阵群控的小木20 小时前
云控系统在TikTok多账号管理中的核心应用与技术实现
开发语言·php·开源软件·个人开发·tk矩阵
傻啦嘿哟21 小时前
降低首字延迟(TTFB):专线节点与TCP Fast Open的配置
开发语言·php
介一安全1 天前
【漏洞学习】PHP+Windows环境通用文件上传漏洞深度剖析
windows·web安全·php·文件上传·安全性测试