tp5 自动生成 sitemap 网站地图

澹台星剑
2023-12-01

撒拉嘿!酒肉穿肠过,久坐板凳上,要说不迈腿,肚子自然凸。

1、生成 sitemap.txt 网站地图

function sitemap() {
    $domain = "https://domain.com";
    $txtmap = "";
    $listData = ["/","/admin","/demo","/test",];
    foreach ($listData as $l){
        $txtmap .= $domain.$l."\n";
    }
    try{
        $fileUrl = 'sitemap.txt';
        $txtfurl = fopen($fileUrl, "w");
        fwrite($txtfurl, $txtmap);
        fclose($txtfurl);
    }catch (Exception $e){
        return json(["code"=>400,"msg"=>"出错了"]);
    }
    return json(["code"=>200,"msg"=>"网站地图获取成功"]);
}

2、生成 sitemap.xml 网站地图

function sitemap() {
    $domain = "https://domain.com";
    $listData = ["/","/admin","/demo","/test",];
    $xmlmap = '<?xml version="1.0" encoding="utf-8" ?>';
    $xmlmap .="<urlset>";
    foreach ($listData as $l){
        $xmlmap .= "<url>";
        $xmlmap .= "<loc>".$domain.$l."</loc>";
        $xmlmap .= "<priority>1.0</priority>";
        $xmlmap .= "<changefreq>daily</changefreq>";
        $xmlmap .= "<lastmod>".date("Y-m-d")."</lastmod>";
        $xmlmap .= "</url>";
    }
    $xmlmap .="</urlset>";
    try{
        $fileUrl = 'sitemap.xml';
        $xmlfurl = fopen($fileUrl, "w");
        fwrite($xmlfurl, $xmlmap);
        fclose($xmlfurl);
    }catch (Exception $e){
        return json(["code"=>400,"msg"=>"出错了"]);
    }
    return json(["code"=>200,"msg"=>"网站地图获取成功"]);
}

骚嘿!每天30个开合跳,5分钟半支撑

当然也可以使用另一种方法生成 sitemap.xml 

参考:https://github.com/mimvp/mimvp-sitemap-php

 类似资料: