网站建设好后一般需要去推广优化,才能获得更多的流量,至于生成网站地图,是为了让各大搜索引擎去发现网站的实时更新,从而更好的收录网站内容;当然了,生成好地图,是需要您去到各大搜索引擎站点去提交的,至于如何提交,请自行网上搜搜。
下面就来给大家介绍php如何生成网站地图sitemap.xml文件:
<?php
//前面两行为数据库连接与查询代码,请根据自己的需求设置
require_once("./mysql/connection.php");
$thread = M()->table("mac_vod")->where("vod_status=1")->order("ORDER BY vod_id DESC")->limit(300)->select("vod_id");
// 创建一个DOMDocument对象
$dom = new DOMDocument('1.0','utf-8');
// 创建根节点
$dom->formatOutput = true;
$root = $dom->createElement("urlset");
$dom->appendChild($root);
foreach($thread as $value){
// 建立根下子节点track
$track = $dom->createElement("url");
$root->appendChild($track);
// 建立track节点下元素
$loc = $dom->createElement("loc");
$track->appendChild($loc);
$priority = $dom->createElement("priority");
$track->appendChild($priority);
$lastmod = $dom->createElement("lastmod");
$track->appendChild($lastmod);
$changefreq = $dom->createElement("changefreq");
$track->appendChild($changefreq);
// 赋值https://www.1901tv.com/vod/detail/id/41980.html
$text = $dom->createTextNode('https://www.1901tv.com/vod/detail/id/'.$value['vod_id'].'.html');
$loc->appendChild($text);
$date = date("Y-m-d",time());
$text = $dom->createTextNode($date);
$lastmod->appendChild($text);
$text = $dom->createTextNode("daily");
$changefreq->appendChild($text);
$text = $dom->createTextNode(0.8);
$priority->appendChild($text);
}
header("Content-type: text/xml");
echo $dom->saveXML();
//保存文件至对应路径,一般保存在网站根目录
$dom->save("../sitemap.xml");
?>