前一篇讲到了如何将资源本地化,这篇博文将讲述如何资源本地化的同时实时更新。
二级页面,文章页面要实时更新其实没有什么必要,只要定时或者稳定间隔时间更新页面就行了。方法有如下几种:
1,利用cookie或者session定时或者间隔时间重新生成页面和文件
2,当生成的文件大小超过一定量(如150M)时,删除文件重新生成。
由于我用的是虚拟空间,以节约成本角度出发我用的是第二种方法。
下面几个方法能达到要求:
public function content(){
//session_start();
$address = ltrim($this->address, '/');
if(file_exists($address))
{//echo $this->address;exit;
/*$session_name = "cache_time_html";
if($_COOKIE[$session_name])
{
$show_data = '';
$grab_caches_time = Cookie::get($session_name);
if((time() - $grab_caches_time) > $this->cache_time_html)
{//echo time() - $grab_caches_time;exit;
$_COOKIE[$session_name] = null;
unset($_COOKIE[$session_name]);
self::creat_html();
}
else
{//echo time() - $grab_caches_time;exit;
$show_data = @file_get_contents(ltrim($this->address, '/'));
print_r($show_data);
exit;
}
}
else
{
Cookie::set($session_name, time());
self::creat_html();
}*/
$show_data = @file_get_contents(ltrim($this->address, '/'));
print_r($show_data);
exit;
}
else
{ //echo 2;exit;
self::creat_html();
}
}
/**
* 生成静态页面
*/
private function creat_html()
{
//echo $this->html_address;exit;
$content = $this->grap->curl_get_content($this->html_address);
if(!$content) $content = @file_get_contents($this->html_address);
if($content)
{
//替换删除内容
$param1 = '/<div\s+?class="w">\s+?友情链接:(.*)<\/div>/isU';
$param2 = '/<form action="" method="post" id="Re_comment" name="CatRecom">(.*)<\/form>/isU';
$param3 = '/<div class="none">(.*)<\/div>/isU';
$param = array($param1, $param2, $param3);
$this->grap->replace_preg($content, $param);
$replace = array('网站导航', '收藏本站', '[<a href="/login.html">登陆</a>|<a href="/register.html">注册</a>]');
$this->grap->replace_preg($content, $replace, '1', '0');
//保存图片到本地
$image_place = array('');
$replace = array('' => '');
$this->grap->get_image($content, $this->sorce_url, "/public/images", $image_place, $replace);
//保存css到本地
$this->grap->get_css($content, $this->sorce_url, "/public/images");//print_r($content);exit;
//保存js到本地
$this->grap->get_js($content, $this->sorce_url, "/public/images");
//处理页面超链接
self::href_oprate($content);
//生成文件夹
$this->grap->get_file_adress($this->address);
$address = ltrim($this->address, '/');
/*$files = fopen($address, 'w');
fwrite($file, $content);
fclose($file);*/
@file_put_contents($address, $content);
print_r($content);exit;
}
else
{
echo '没有获取到页面内容,请刷新重试!';
}
}
/**
* 处理页面超链接
*
* @param srting $content
*/
private function href_oprate(&$content)
{
$content = str_replace($this->sorce_url, '', $content);
$zx_patton = array(
'',
);
$zx_replace = array(
'',
);
$content = str_replace($zx_patton, $zx_replace, $content);
@preg_match_all("/<a([^>]*)/i",$content,$match);
$href = $match['1'];
foreach ($href as $key => $val)
{
if(!stristr($val, 'href')) continue;
if(stristr($val, "http://") || stristr($val, "https://") || stristr($val, "/login.html") || stristr($val, "/register.html") || stristr($val, ".php"))
{
$content = str_replace($val, '', $content);
}
if(!stristr($val, "javascript"))
{
$fg = stristr($val, 'href="') ? 'href="' : "href='";
$arr_url = explode($fg, $val);
$arr_url['1'] = trim($arr_url['1']);
if(substr($arr_url['1'], 0, 1) == '"' && substr($arr_url['1'], 0, 1) == "'") continue;
if(substr($arr_url['1'], 0, 1) == '/')
{
if(substr($arr_url['1'], 0, 2) !== '/"' && substr($arr_url['1'], 0, 2) !== "/'")
{
$arr_url['1'] = substr($arr_url['1'], 0, 1) != '/' ? '/html/'.$arr_url['1'] : '/html'.$arr_url['1'] ;
}
$new_url = implode($arr_url, $fg);//echo $val.'||'.$new_url.'<hr>';
$content = str_replace($val, $new_url, $content);
}
}
}//exit;
}
实例演示: http://news.80jiaju.com/