这篇文章主要介绍了如何使用php生成sitemap,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
创新互联-专业网站定制、快速模板网站建设、高性价比郊区网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式郊区网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖郊区地区。费用合理售后完善,十载实体公司更值得信赖。
class SiteMapController extends Controller { /** * ==============================================XML 格式===========================================================*/ 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 = "\n"; $xmlTemplate .= " http://www.yoursite.com/yoursite.html 2009-12-14 daily 0.8 http://www.yoursite.com/yoursite2.html 2010-05-01 daily 0.8 \n"; foreach ($allUrlData as $val){ $xmlTemplate .= " \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 = "\n"; $xmlTemplate .= "\n"; $xmlTemplate .= "\n"; $xmlTemplate .= "\n"; $xmlTemplate .= "\n"; if(mb_strpos($val['url'],'&') !== false){ $val['url'] = str_replace('&','&',$val['url']); } $xmlTemplate .= " \n"; } $xmlTemplate .= "".$val['url']." \n"; $xmlTemplate .= "".date('Y-m-d')." \n"; $xmlTemplate .= "daily \n"; $xmlTemplate .= "".$val['weight']." \n"; $xmlTemplate .= "\n"; $xmlTemplate .= "\n"; $xmlTemplate .= "\n"; $xmlTemplate .= "\n"; $xmlTemplate .= " \n"; foreach ($allUrlData as $val){ $xmlTemplate .= "
\n"; $xmlTemplate .= "\n"; $siteMapXML = fopen("sitemap.html", "w+") or die("无法写入sitemap.html"); fwrite($siteMapXML, $xmlTemplate); fclose($siteMapXML); } }- ".$val['name']."
\n"; } $xmlTemplate .= "
感谢你能够认真阅读完这篇文章,希望小编分享的“如何使用php生成sitemap”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,更多相关知识等着你来学习!