业务需要是要生成sitemap,现在公司使用的框架时thinkphp5,想用tp5做sitemap这个功能
生成sitemap其实不难,按照sitemap xml格式将站点链接写入即可
function sitemap() {
$domain = "https://domain.com";
$listData = ["/","/index.html","/index.html?id=2","/index.html?id=3",];
$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"=>"网站地图获取成功"]);
}
楼主你这个问题解决没,我现在也遇上了这个问题,如果可以的话麻烦提示一下