福州网站建设>网站新闻>php技术

php sitemap.xml文件生成源码 sitemap.xml生成方法 亲试可用 2023 2022

发布日期:2022-12-24浏览次数:356 来源:福州网站建设

$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"文章,如果你在这方面有什么问题,随时联系我们

php技术有关的文章
如果您有什么问题,欢迎咨询我们客服! 点击QQ咨询