Laravel5.8 生成网站XML地图,案例,例子。
<?php public function sore(){ $xml = $this->sitemap(); $file = fopen('sitemap.xml', 'w'); fwrite($file, $xml); fclose($file); $server='http://'.$_SERVER['SERVER_NAME'].'/sitemap.xml'; return response()->json(['code'=>200,'msg'=>'成功生成网站地图','url'=>route('admin.sitemap.index'),'data'=>$server]); } private function sitemap() { $xml = '<?xml version="1.0" encoding="utf-8"?>'."\n"; $xml .= '<urlset>'."\n"; // 数据 $vod=DB::table('vod')->get(); foreach($vod as $data) { $xml .= $this->execute_xml('/vod/detail/'.$data->id)."\n"; } $xml .= '</urlset>'."\n"; return $xml; } private function execute_xml($url) { $xml_url = '<url>'."\n"; $xml_url .= '<loc>http://'. $_SERVER['SERVER_NAME'] . $url.'.html' .'</loc>'."\n"; $xml_url .= '<lastmod>'. date("Y-m-d", time()) .'</lastmod>'."\n"; $xml_url .= '<changefreq>weekly</changefreq>'."\n"; $xml_url .= '<priority>0.8</priority>'."\n"; $xml_url .= '</url>'."\n"; return $xml_url; } ?>
文章评论(0)