网站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;
?>
相关推荐
BingoGo2 天前
PHP 泛型之殇 泛型 RFC 提案被拒绝
后端·php
JaguarJack2 天前
PHP 泛型之殇 泛型 RFC 提案被拒绝
后端·php
用户3074596982072 天前
PHP 扩展——从入门到理解
php
鹏仔先生3 天前
拷贝漫画APP下载页PHP程序,后台带免费AI写作
php
云水一下3 天前
从零开始学 PHP 系列(一):PHP 的前世今生与开发环境搭建
开发语言·php
xingpanvip3 天前
星盘接口开发文档:本命盘接口指南
android·开发语言·css·php·lua
酉鬼女又兒3 天前
零基础入门计算机网络运输层:端到端通信核心作用、端口号分类规则、复用分用工作机制及UDP与TCP协议全方位对比详解
网络·网络协议·tcp/ip·计算机网络·考研·udp·php
dog2503 天前
不要再继续优化 TCP
网络协议·tcp/ip·php
Channing Lewis3 天前
PHP 解析 Excel 的那些坑:一次“行号错位”引发的数据丢失
开发语言·php·excel
Cheng小攸4 天前
渗透行为分析与检测
开发语言·php