結合設計經驗與營銷實踐,提供有價值的互聯網資訊
發布日期:2022-12-24瀏覽次數:634 來源:福州網站建設
$thread=array(
array("id"=>1),
array("id"=>2),
array("id"=>3),
);
$url="http://www.baidu.com";
$suffix=".html";
sitemap($thread, $url, $suffix);
function sitemap($thread, $url, $suffix, $filename = 'sitemap.xml')
{
// 建立一個DOMDocument對象
$dom = new \DOMDocument("1.0", "utf-8");
header("Content-Type: text/xml");
// 建立根節點
$root = $dom->createElement("urlset");
$root->setAttribute( "xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9" );
$root->setAttribute( "xmlns:mobile", "http://www.baidu.com/schemas/sitemap-mobile/1/" );
$dom->appendChild($root);
foreach ($thread as $value) {
// 創建根下子節點track
$track = $dom->createElement("url");
$root->appendChild($track);
// 創建track節點下元素
$loc = $dom->createElement("loc");
$track->appendChild($loc);
$mobile = $dom->createElement("mobile:mobile");
$mobile->setAttribute("type","pc,mobile");
$track->appendChild($mobile);
$priority = $dom->createElement("priority");
$track->appendChild($priority);
$lastmod = $dom->createElement("lastmod");
$track->appendChild($lastmod);
$changefreq = $dom->createElement("changefreq");
$track->appendChild($changefreq);
// 賦值
$content = $url . $value['id'] . $suffix;
$text = $dom->createTextNode($content);
$loc->appendChild($text);
$date = "2022-02-02";
$text = $dom->createTextNode($date);
$lastmod->appendChild($text);
$text = $dom->createTextNode('daily');
$changefreq->appendChild($text);
$text = $dom->createTextNode(0.8);
$priority->appendChild($text);
}
//生成xml文件
$dom->save($filename);
}
以上是由福州網站建設的小編為你分享了"php sitemap.xml文件生成源碼 sitemap.xml生成方法 親試可用 2023 2022"文章,如果你在這方面有什么問題,隨時聯系我們