以下文件仅供参考学习,具体数据结构和页面地址根据实际情况变化:
实现逻辑:
1、fle_get_contents 得到页面的内容
2、通过正则将页面中的url地址替换为【列表、文章】相符合的地址
3、按生成路径写入指定文件夹
4、前台打开时直接定向至生成目录
<?php
namespace app\admin\controller;
use library\Controller;
use library\tools\Data;
use think\Db;
class Generate extends Controller
{
/*****************
说明:首页生成
****************/
public function index(){
if ($this->request->isGet()) {
$this->title = '生成首页';
$this->fetch();
}else{
//生成首页HTML
$host = $_SERVER["HTTP_HOST"];
$url = 'http://'.$host.'/index.php';
$dir = './';
$filename = 'index';
$content = file_get_contents($url);
//处理页面链接
$listurls = '/\/index.php\/index\/index\/(.*?).html\?id=(.*?)\">/';
preg_match_all($listurls,$content,$matchlist);
foreach($matchlist[1] as $key => $value){
$value = trim($value);
$listurl = '/index.php/index/html/'.$value.'/'.$matchlist[2][$key].'.html">';
$content = str_replace($matchlist[0][$key], $listurl, $content);
}
$content = str_replace("/index.php/index/index", 'html', $content);
$content = str_replace("/index.php/index", '', $content);
//生成文件
$path = $dir."/".$filename.".html";
echo $this->set_url_content($content, $path);
}
}
/*****************
说明:列表页生成
****************/
public function column(){
if ($this->request->isGet()) {
$this->title = '生成栏目';
$this->fetch();
}else{
$col_name = input('col_name');
$column = input('column');
if($col_name){
$host = $_SERVER["HTTP_HOST"];
$url = 'http://'.$host.'/index.php/index/index/'.$col_name.'.html';
$dir = './html/';
$filename = $col_name;
$content = file_get_contents($url);
//处理页面链接
if($col_name == 'news' || $col_name == 'story'){
$listurls = '/\/index.php\/index\/index\/(.*?).html\?id=(.*?)" class/';
}else{
$listurls = '/\/index.php\/index\/index\/(.*?).html\?id=([0-9]+)[\"|\" ].*?>/';
}
preg_match_all($listurls,$content,$matchlist);
foreach($matchlist[1] as $key => $value){
$value = trim($value);
if($col_name == 'news' || $col_name == 'story'){
$listurl = '/index.php/index/html/'.$value.'/'.$matchlist[2][$key].'.html" class';
}else{
$listurl = '/index.php/index/html/'.$value.'/'.$matchlist[2][$key].'.html">';
}
$content = str_replace($matchlist[0][$key], $listurl, $content);
}
$content = str_replace("/index.php/index/index", '/html', $content);
$content = str_replace("/index/index", '/html', $content);
$content = str_replace("/index.php/index", '', $content);
//处理下一页
$rexp_page = '/\/html\/'.$col_name.'.html\?page=([0-9]+)\"\>下一页\<\/a\>/';
preg_match_all($rexp_page, $content, $matchlist);
foreach($matchlist[0] as $key => $value){
$value = trim($value);
$listurl = '/html/'.$col_name.'_'.$matchlist[1][$key].'.html">下一页</a>';
$content = str_replace($value, $listurl, $content);
}
//生成文件
$path = $dir."/".$filename.".html";
$ret = [];
$ret['status'] = $this->set_url_content($content, $path);
$ret['page'] = '/html/'.$filename.".html";
$ret['column'] = $column;
echo json_encode($ret);
}
}
}
/*****************
说明:文章页生成
****************/
public function article(){
if ($this->request->isGet()) {
$this->title = '生成文章页';
$this->fetch();
}else{
$col_name = input('col_name');
$column = input('column');
$ret['status'] = 1;
$count = db('article')->count();
$allpage = ceil($count/20);
$page = input('page', 1);
$offset = ($page-1)*20;
$this->page_generate($offset, $page);
if($page >= $allpage){
$ret['end'] = 1;
}else{
$ret['end'] = 0;
}
$page++;
$ret['page'] = $page;
echo json_encode($ret);
}
}
/*****************
说明:设置文章页生成
****************/
public function page_generate($offset, $page){
$rows = db('article')->order('id DESC')->limit($offset, 20)->select();
$host = $_SERVER["HTTP_HOST"];
$dir = './html/article/';
if (!is_dir($dir)){ //如果目录不存在
mkdir(iconv("UTF-8", "GBK", $dir),0777,true); //创建目录(777权限)
}
//处理分页页面
$this->handle_designer_page($page);
foreach($rows as $itm){
$url = 'http://'.$host.'/index.php/index/index/profile.html?id='.$itm['id'];
$content = file_get_contents($url);
//找到页面的链接
$listurls = '/\/index.php\/index\/index\/(.*?).html\?id=([0-9]+).*?>/';
preg_match_all($listurls,$content,$matchlist);
foreach($matchlist[1] as $key => $value){
$value = trim($value);
$listurl = '/index.php/index/html/'.$value.'/'.$matchlist[2][$key].'.html">';
$content = str_replace($matchlist[0][$key], $listurl, $content);
}
$content = str_replace("/index.php/index/index", '/html', $content);
$content = str_replace("/index.php/index", '', $content);
$path = $dir."/".$itm['id'].".html";
$this->set_url_content($content, $path);
}
}
/*****************
说明:设置静态内容
****************/
public function set_url_content($content, $path){
if(file_exists($path)){
unlink($path);
}
$fp = fopen($path, "w");
$flag = fwrite($fp, $content); //写入内容
fclose($fp);
if(file_exists($path)){
return 1;
}else{
return -1;
}
}
/*****************
说明:处理文章页面分页
****************/
public function handle_designer_page($page){
$host = $_SERVER["HTTP_HOST"];
//处理分页页面
$page_url = 'http://'.$host.'/index.php/index/index/designer.html?page='.$page;
$content = file_get_contents($page_url);
$page_path = './html/designer_'.$page.".html";
//第1步:处理页面content
$listurls = '/\/index.php\/index\/index\/(.*?).html\?id=([0-9]+)[\"|\" ].*?>/';
preg_match_all($listurls,$content,$matchlist);
foreach($matchlist[1] as $key => $value){
$value = trim($value);
$listurl = '/index.php/index/html/'.$value.'/'.$matchlist[2][$key].'.html">';
$content = str_replace($matchlist[0][$key], $listurl, $content);
}
$content = str_replace("/index.php/index/index", '/html', $content);
$content = str_replace("/index.php/index", '', $content);
//第2步:处理上一页下一页
//处理下一页
$rexp_page = '/\/html\/designer.html\?page=([0-9]+)\"\>下一页\<\/a\>/';
preg_match_all($rexp_page, $content, $matchlist);
foreach($matchlist[0] as $key => $value){
$value = trim($value);
$listurl = '/html/designer_'.$matchlist[1][$key].'.html">下一页</a>';
$content = str_replace($value, $listurl, $content);
}
//处理上一页
$rexp_page = '/\/html\/designer.html\?page=([0-9]+)\"\>上一页\<\/a\>/';
preg_match_all($rexp_page, $content, $matchlist);
foreach($matchlist[0] as $key => $value){
$value = trim($value);
$listurl = '/html/designer_'.$matchlist[1][$key].'.html">上一页</a>';
$content = str_replace($value, $listurl, $content);
}
$this->set_url_content($content, $page_path);
}
.....
}