php生成百度sitemap.xml,简单方便seo
·
//创建百度 xml
public function createBaiduMap() {
// 创建一个DOMDocument对象
$dom = new DOMDocument('1.0','utf-8');
// 创建根节点
$dom ->formatOutput = true;
//设置urlset
$urlset = $dom ->createElement("urlset");
//xmlns
$xmlns = $dom ->createAttribute('xmlns');
$xmlnsvalue = $dom ->createTextNode("http://www.sitemaps.org/schemas/sitemap/0.9");
$xmlns -> appendChild($xmlnsvalue);
$urlset ->appendChild($xmlns);
//xmlns:mobile
$xmlns_mobile = $dom ->createAttribute('xmlns:mobile');
$xmlns_mobilevalue = $dom ->createTextNode("http://www.baidu.com/schemas/sitemap-mobile/1/");
$xmlns_mobile -> appendChild($xmlns_mobilevalue);
$urlset ->appendChild($xmlns_mobile);
$dom ->appendChild($urlset);
//要提交百度的地址
$list = [
"https://www.abc.cn",
"https://www.abcd.cn",
];
foreach ($list as $k => $v) {
// 建立根下子节点track
$track = $dom ->createElement("url");
$urlset ->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);
$mobile_mobile = $dom ->createElement("mobile:mobile");
$type = $dom ->createAttribute('type');
$typevalue = $dom ->createTextNode("pc,mobile");
$type -> appendChild($typevalue);
$mobile_mobile ->appendChild($type);
$track ->appendChild($mobile_mobile);
$text = $dom ->createTextNode($v);
$loc ->appendChild($text);
$date = date("Y-m-d",time());
$text = $dom ->createTextNode($date);
$lastmod ->appendChild($text);
$text = $dom ->createTextNode("weekly");
$changefreq ->appendChild($text);
$text = $dom ->createTextNode(1);
$priority ->appendChild($text);
}
$dom ->save(."/sitemap.xml"); //保存路径 绝对路径
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)