<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Get_contents extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->database('default');
}
public function index(){
$this->load->view("get_contents/get_curl.html");
}
/**
* 接受地址
*/
public function get_address(){
$url=$this->input->post("address");
$this->load->library('Curl');
// print_r($url);die;
$ch=curl_init();
$params[CURLOPT_URL ]=$url;
//是否返回响应头信息
$params[CURLOPT_HEADER ]=true;
//是否将结果返回
$params[CURLOPT_RETURNTRANSFER ]=true;
//是否重定向
$params[CURLOPT_FOLLOWLOCATION ]=true;
//伪造浏览器
$params[CURLOPT_USERAGENT ] = 'Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1';
//开始发送请求传入curl
curl_setopt_array($ch,$params);
$content=curl_exec($ch);
$preg='#<li>.*<div class="img fL">.*<a href=".*" target="_blank">.*<img src="(.*)" alt=".*" />.*</a>.*</div>.*<div class="info">.*<h4 class="title">.*<a href=".*" target="_blank">(.*)</a>.*</h4>.*<p>(.*)</p>.*<div class="con clearfix">.*<span class="fR">.*<a href=".*" target="_blank">.*</a></span>.*<span class="docs">.*</span>.*</div>.*<div class="con clearfix">.*<span class="time">.*</span>.*</div>.*</div>.*</li>#isU';
$arr=$this->curl->get_info($url,$preg);
preg_match_all($preg,$content,$arr);
$path='./public/img/';
for ($i=0; $i <count($arr[1]); $i++) {
$res=@file_get_contents($arr[1][$i]);
$img_name=substr($arr[1][$i],strrpos($arr[1][$i],"."));
$img_path="./public/img/".time().rand(1,9999).mt_rand().$img_name;
$arr[1][$i]=$img_path;
file_put_contents($img_path,$res);
}
$info['img']=$arr[1];
$info['title']=$arr[2];
$info['contents']=$arr[3];
// mysql_connect("127.0.0.1","root","root");
// mysql_select_db("seven_month");
$dsn="mysql:host=127.0.0.1;dbname=seven_month";
$db=new PDO($dsn,'root','root');
// mysql_query("set names utf8");
$sql="insert into get_contents(img,title,contents) values";
for ($i=0; $i<count($info['img']); $i++) {
$sql.="('".$info['img'][$i]."','".$info['title'][$i]."','".$info['contents'][$i]."'),";
}
$sql=substr($sql,0,-1);
try{
$this->db->conn_id->beginTransaction();
$res=$this->db->conn_id->exec($sql);
// print_r($res);die;
if(!$res){
throw new PDOException("添加失败");
}
// echo "添加成功";
$this->db->conn_id->commit();
}catch(PDOException $a){
$this->db->conn_id->rollback();
exit($a->getMessage());
}
if($res){
$title="采集";
$user="";
$address=substr($url,0,-1);
$contents="采集了".count($arr[1])."条数据,点击查看".$address;
$myaddress="@163.com";
$this->send_email($title,$user,$contents,$myaddress);
}else{
echo "失败";
}
}
/**
* 邮箱发送
*/
public function send_email($title,$user,$contents,$myaddress){
$this->load->library('email');
$config['protocol']='smtp';
$config['smtp_host']='smtp.163.com';
$config['smtp_user']='@163.com';
$config['smtp_pass']='';//这个163邮箱的那个验证码
$config['mailtype']='html';
$config['smtp_port']=25;
$config['charset']='utf-8';
$this->email->initialize($config);
$this->email->from("$myaddress", "$user");
$this->email->to($myaddress);
$this->email->cc('@qq.com');//QQ号或者是 163邮箱
$this->email->subject($title);
$this->email->message($contents);
if($this->email->send())
{
header("refresh:3;url=http://127.0.0.1/seven/CI/index.php/Get_contents/get_contents_show");
echo '正在加载,请稍等...<br>三秒后自动跳转';
}
else
{
echo $this->email->print_debugger();
}
}
public function get_contents_show(){
ob_start();
if(file_exists('D:/phpStudy/WWW/seven/CI/application/views/get_contents/'.date("Ymd").'.html')){
$this->load->view('get_contents/'.date("Ymd").'.html');die;
}
$lin="mysql:host=127.0.0.1;dbname=seven_month";
$pdo=new PDO($lin,'root','root');
$pdo->exec("set names utf8");
$res=$pdo->query("select * from get_contents");
$result=$res->fetchAll(PDO::FETCH_ASSOC);
$this->load->vars('result',$result);
$this->load->view('Get_contents/get_contents_shows.html');
$cacheTime = 864000; //设置缓存页面过期时间
$cacheDir = 'D:/phpStudy/WWW/seven/CI/application/views/get_contents/'; //设置缓存页面文件目录
if (!is_dir($cacheDir))
mkdir($cacheDir);//判断目录是否存在,否则创建目录
$cacheFile = $cacheDir.'/'.(int)date("Ymd").'.html'; //缓存文件路径,文件以日期命名
if (!is_file($cacheFile) || time() - filemtime($cacheFile) > $cacheTime) {
$content = ob_get_contents(); //取得php页面输出的全部内容
$fp = fopen($cacheFile, "w"); //输出内容写入文件
fwrite($fp, $content);
fclose($fp);
} else {
echo $content = file_get_contents($cacheFile); //如果缓存文件已经存在,且未过期则读取
}
}
}
defined('BASEPATH') OR exit('No direct script access allowed');
class Get_contents extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->database('default');
}
public function index(){
$this->load->view("get_contents/get_curl.html");
}
/**
* 接受地址
*/
public function get_address(){
$url=$this->input->post("address");
$this->load->library('Curl');
// print_r($url);die;
$ch=curl_init();
$params[CURLOPT_URL ]=$url;
//是否返回响应头信息
$params[CURLOPT_HEADER ]=true;
//是否将结果返回
$params[CURLOPT_RETURNTRANSFER ]=true;
//是否重定向
$params[CURLOPT_FOLLOWLOCATION ]=true;
//伪造浏览器
$params[CURLOPT_USERAGENT ] = 'Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1';
//开始发送请求传入curl
curl_setopt_array($ch,$params);
$content=curl_exec($ch);
$preg='#<li>.*<div class="img fL">.*<a href=".*" target="_blank">.*<img src="(.*)" alt=".*" />.*</a>.*</div>.*<div class="info">.*<h4 class="title">.*<a href=".*" target="_blank">(.*)</a>.*</h4>.*<p>(.*)</p>.*<div class="con clearfix">.*<span class="fR">.*<a href=".*" target="_blank">.*</a></span>.*<span class="docs">.*</span>.*</div>.*<div class="con clearfix">.*<span class="time">.*</span>.*</div>.*</div>.*</li>#isU';
$arr=$this->curl->get_info($url,$preg);
preg_match_all($preg,$content,$arr);
$path='./public/img/';
for ($i=0; $i <count($arr[1]); $i++) {
$res=@file_get_contents($arr[1][$i]);
$img_name=substr($arr[1][$i],strrpos($arr[1][$i],"."));
$img_path="./public/img/".time().rand(1,9999).mt_rand().$img_name;
$arr[1][$i]=$img_path;
file_put_contents($img_path,$res);
}
$info['img']=$arr[1];
$info['title']=$arr[2];
$info['contents']=$arr[3];
// mysql_connect("127.0.0.1","root","root");
// mysql_select_db("seven_month");
$dsn="mysql:host=127.0.0.1;dbname=seven_month";
$db=new PDO($dsn,'root','root');
// mysql_query("set names utf8");
$sql="insert into get_contents(img,title,contents) values";
for ($i=0; $i<count($info['img']); $i++) {
$sql.="('".$info['img'][$i]."','".$info['title'][$i]."','".$info['contents'][$i]."'),";
}
$sql=substr($sql,0,-1);
try{
$this->db->conn_id->beginTransaction();
$res=$this->db->conn_id->exec($sql);
// print_r($res);die;
if(!$res){
throw new PDOException("添加失败");
}
// echo "添加成功";
$this->db->conn_id->commit();
}catch(PDOException $a){
$this->db->conn_id->rollback();
exit($a->getMessage());
}
if($res){
$title="采集";
$user="";
$address=substr($url,0,-1);
$contents="采集了".count($arr[1])."条数据,点击查看".$address;
$myaddress="@163.com";
$this->send_email($title,$user,$contents,$myaddress);
}else{
echo "失败";
}
}
/**
* 邮箱发送
*/
public function send_email($title,$user,$contents,$myaddress){
$this->load->library('email');
$config['protocol']='smtp';
$config['smtp_host']='smtp.163.com';
$config['smtp_user']='@163.com';
$config['smtp_pass']='';//这个163邮箱的那个验证码
$config['mailtype']='html';
$config['smtp_port']=25;
$config['charset']='utf-8';
$this->email->initialize($config);
$this->email->from("$myaddress", "$user");
$this->email->to($myaddress);
$this->email->cc('@qq.com');//QQ号或者是 163邮箱
$this->email->subject($title);
$this->email->message($contents);
if($this->email->send())
{
header("refresh:3;url=http://127.0.0.1/seven/CI/index.php/Get_contents/get_contents_show");
echo '正在加载,请稍等...<br>三秒后自动跳转';
}
else
{
echo $this->email->print_debugger();
}
}
public function get_contents_show(){
ob_start();
if(file_exists('D:/phpStudy/WWW/seven/CI/application/views/get_contents/'.date("Ymd").'.html')){
$this->load->view('get_contents/'.date("Ymd").'.html');die;
}
$lin="mysql:host=127.0.0.1;dbname=seven_month";
$pdo=new PDO($lin,'root','root');
$pdo->exec("set names utf8");
$res=$pdo->query("select * from get_contents");
$result=$res->fetchAll(PDO::FETCH_ASSOC);
$this->load->vars('result',$result);
$this->load->view('Get_contents/get_contents_shows.html');
$cacheTime = 864000; //设置缓存页面过期时间
$cacheDir = 'D:/phpStudy/WWW/seven/CI/application/views/get_contents/'; //设置缓存页面文件目录
if (!is_dir($cacheDir))
mkdir($cacheDir);//判断目录是否存在,否则创建目录
$cacheFile = $cacheDir.'/'.(int)date("Ymd").'.html'; //缓存文件路径,文件以日期命名
if (!is_file($cacheFile) || time() - filemtime($cacheFile) > $cacheTime) {
$content = ob_get_contents(); //取得php页面输出的全部内容
$fp = fopen($cacheFile, "w"); //输出内容写入文件
fwrite($fp, $content);
fclose($fp);
} else {
echo $content = file_get_contents($cacheFile); //如果缓存文件已经存在,且未过期则读取
}
}
}