php生成网站地图sitemap.xml文件

网站建设好后一般需要去推广优化,才能获得更多的流量,至于生成网站地图,是为了让各大搜索引擎去发现网站的实时更新,从而更好的收录网站内容;当然了,生成好地图,是需要您去到各大搜索引擎站点去提交的,至于如何提交,请自行网上搜搜。

下面就来给大家介绍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");
?>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值