这篇文章主要介绍了如何使用php生成sitemap,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
class SiteMapController extends Controller { /** * ==============================================XML 格式=========================================================== <?xml version="1.0" encoding="utf-8"?> <!-- XML文件需以utf-8编码--> <urlset> <!--必填标签--> <url> <!--必填标签,这是具体某一个链接的定义入口,每一条数据都要用<url>和</url>包含在里面,这是必须的 --> <loc>http://www.yoursite.com/yoursite.html</loc> <!--必填,URL链接地址,长度不得超过256字节--> <lastmod>2009-12-14</lastmod> <!--可以不提交该标签,用来指定该链接的最后更新时间--> <changefreq>daily</changefreq> <!--可以不提交该标签,用这个标签告诉此链接可能会出现的更新频率 --> <priority>0.8</priority> <!--可以不提交该标签,用来指定此链接相对于其他链接的优先权比值,此值定于0.0-1.0之间--> </url> <url> <loc>http://www.yoursite.com/yoursite2.html</loc> <lastmod>2010-05-01</lastmod> <changefreq>daily</changefreq> <priority>0.8</priority> </url> </urlset> */ private function cateModel(){ return new Cate(); } private function articleModel(){ return new Article(); } /** * */ public function createSiteMap(){ $allUrlData = array_merge($this->mastUrl(),$this->getADList(),$this->getArticleList(),$this->getCateList()); // prt(count($allUrlData)); $allUrlData = array_unique($allUrlData, SORT_REGULAR); // prt(count($allUrlData)); $this->createSiteMapXML($allUrlData); // $this->createSiteMapTxt($allUrlData); $this->createSiteMapHtml($allUrlData); } /** * @return array */ private function mastUrl(){ return []; } /** * 广告数据 */ private function getADList(){ $adPosList = $this->adModel()->adPosUrlList(); $adInfoList = $this->adModel()->adInfoUrlList(); $adUrlList = array_merge($adPosList,$adInfoList); array_walk($adUrlList,function (&$val){ $val['weight'] = 0.8; }); return $adUrlList; } /** * 文章数据 */ private function getArticleList(){ $articleUrlList = $this->articleModel()->articleUrlList(); $articleTagUrlList = $this->articleModel()->articleTagUrlList(); $articleUrlList = array_merge($articleUrlList,$articleTagUrlList); array_walk($articleUrlList,function (&$val){ $val['weight'] = 0.8; }); return $articleUrlList; } /** * 栏目数据 */ private function getCateList(){ $cateUrlList = $this->cateModel()->cateUrlList(); array_walk($cateUrlList,function (&$val){ $val['weight'] = 0.8; }); return $cateUrlList; } /** * @param $allUrlData */ private function createSiteMapXML($allUrlData){ if (!$allUrlData){return false;} $xmlTemplate = "<?xml version='1.0' encoding='utf-8'?>\n"; $xmlTemplate .= "<urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>\n"; foreach ($allUrlData as $val){ $xmlTemplate .= "<url>\n"; if(mb_strpos($val['url'],'&') !== false){ $val['url'] = str_replace('&','&',$val['url']); } $xmlTemplate .= "<loc>".$val['url']."</loc>\n"; $xmlTemplate .= "<lastmod>".date('Y-m-d')."</lastmod>\n"; $xmlTemplate .= "<changefreq>daily</changefreq>\n"; $xmlTemplate .= "<priority>".$val['weight']."</priority>\n"; $xmlTemplate .= "</url>\n"; } $xmlTemplate .= "</urlset>\n"; $siteMapXML = fopen("sitemap.xml", "w+") or die("无法写入sitemap.xml"); fwrite($siteMapXML, $xmlTemplate); fclose($siteMapXML); } private function createSiteMapTxt($allUrlData){ if (!$allUrlData){return false;} $txtTemplate = ''; foreach ($allUrlData as $val){ $txtTemplate .= $val['url']."\n"; } $siteMapTxt = fopen("sitemap.txt", "w+") or die("无法写入sitemap.txt"); fwrite($siteMapTxt, chr(0xEF).chr(0xBB).chr(0xBF)); fwrite($siteMapTxt, $txtTemplate); fclose($siteMapTxt); } private function createSiteMapHtml($allUrlData){ if (!$allUrlData){return false;} $xmlTemplate = "<!DOCTYPE html>\n"; $xmlTemplate .= "<html>\n"; $xmlTemplate .= "<head>\n"; $xmlTemplate .= "<meta charset='utf-8'/>\n"; $xmlTemplate .= "<title></title>\n"; $xmlTemplate .= "<style>a{color:rgb(0, 0, 238);font-size:13px;}</style>\n"; $xmlTemplate .= "</head>\n"; $xmlTemplate .= "<body>\n"; $xmlTemplate .= "<ol>\n"; foreach ($allUrlData as $val){ $xmlTemplate .= "<li><a href='".$val['url']."'>".$val['name']."</a></li>\n"; } $xmlTemplate .= "</ol>\n"; $xmlTemplate .= "</html>\n"; $siteMapXML = fopen("sitemap.html", "w+") or die("无法写入sitemap.html"); fwrite($siteMapXML, $xmlTemplate); fclose($siteMapXML); } }
感谢你能够认真阅读完这篇文章,希望小编分享的“如何使用php生成sitemap”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。