tp5 自动生成 sitemap 网站地图

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

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 = '';
    $xmlmap .="";
    foreach ($listData as $l){
        $xmlmap .= "";
        $xmlmap .= "".$domain.$l."";
        $xmlmap .= "1.0";
        $xmlmap .= "daily";
        $xmlmap .= "".date("Y-m-d")."";
        $xmlmap .= "";
    }
    $xmlmap .="";
    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

你可能感兴趣的:(php,thinkPHP5)