福州網(wǎng)站建設(shè)>網(wǎng)站新聞>php技術(shù)

        php sitemap.xml文件生成源碼 sitemap.xml生成方法 親試可用 2023 2022

        發(fā)布日期:2022-12-24瀏覽次數(shù):583 來源:福州網(wǎng)站建設(shè)

        $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')  
        {  
          // 建立一個(gè)DOMDocument對(duì)象  
          $dom = new \DOMDocument("1.0", "utf-8");  
          header("Content-Type: text/xml");  
          // 建立根節(jié)點(diǎn)  
          $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) {  
          // 創(chuàng)建根下子節(jié)點(diǎn)track  
          $track = $dom->createElement("url");  
          $root->appendChild($track);  
          // 創(chuàng)建track節(jié)點(diǎn)下元素  
          $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);    
        }

        以上是由福州網(wǎng)站建設(shè)的小編為你分享了"php sitemap.xml文件生成源碼 sitemap.xml生成方法 親試可用 2023 2022"文章,如果你在這方面有什么問題,隨時(shí)聯(lián)系我們

        php技術(shù)有關(guān)的文章
        如果您有什么問題,歡迎咨詢我們客服! 點(diǎn)擊QQ咨詢