首頁 > php教程 > PHP源码 > 全自动小说订阅微信推送

全自动小说订阅微信推送

PHP中文网
發布: 2016-05-25 17:00:03
原創
1524 人瀏覽過

<?php
header("Content-type: text/html;charset=utf-8");
//set_time_limit(0);

$dbname = SAE_MYSQL_DB;

 $host = SAE_MYSQL_HOST_M;
 $port = SAE_MYSQL_PORT;
 $user = SAE_MYSQL_USER;
 $pwd = SAE_MYSQL_PASS;

$connect = @mysql_connect("{$host}:{$port}",$user,$pwd,true);
if(!$connect) {
 die("Connect Server Failed:". mysql_error());
}

if(!mysql_select_db($dbname,$connect)) {
 die("Select Database Failed:". mysql_error($connect));
}

mysql_query("set names &#39;utf8&#39;");

$rules = array(
&#39;start&#39;=>&#39;http://www.douluodalu.com.cn/jueshitangmen/6860.html&#39;,//开始采集的url
&#39;title&#39;=>&#39;/<h1>(.*?)</h1>/&#39;,//文章title
&#39;time&#39;=>&#39;/发布时间:(.*?) /&#39;,//发布时间
&#39;content&#39;=>&#39;/"></div><p>([sS]*?)<div align=center>/&#39;,//内容
&#39;next&#39;=>&#39;/下一篇: <a href="(.*?)"/&#39;,//下一篇网址
);


//每次排序,取出上一次的最后一篇url
$url = getLatest();

//最后一章的下一篇为空,由此循环
while($url != null && $url !=""){
 $value = get($url);

 $value = _prefilter($value);//去除空白字符,空格,回车
 $context = getContent($value);
$context[&#39;url&#39;] = $url;//当前url,同时还有下一篇的url
 $url = $context[&#39;next&#39;];
var_dump($url);
//防止重复
if(storage($context)){
storageWP($context);
};
}
echo"采集结束";
mysql_close($connect);


function storage($content_array){
global $connect;
$sql ="insert into `articles` (`id`, `title`, `time`, `url`, `content`) values(null,
&#39;{$content_array[&#39;title&#39;]}&#39;,
&#39;{$content_array[&#39;time&#39;]}&#39;,
&#39;{$content_array[&#39;url&#39;]}&#39;,
&#39;{$content_array[&#39;content&#39;]}&#39;);";
$result = mysql_query($sql,$connect);
return $result;
}

function storageWP($content_array){
global $connect;
$result = mysql_query("select max(ID) from wp_posts;",$connect);

$row = mysql_fetch_row($result);
$last_id = $row[0] +1 ;
$sql ="INSERT INTO `wp_posts` (`ID`, `post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`, `post_excerpt`, `post_status`, `comment_status`, `ping_status`, `post_password`, `post_name`, `to_ping`, `pinged`, `post_modified`, `post_modified_gmt`, `post_content_filtered`, `post_parent`, `guid`, `menu_order`, `post_type`, `post_mime_type`, `comment_count`) VALUES (null,1,&#39;{$content_array[&#39;time&#39;]}&#39;, &#39;{$content_array[&#39;time&#39;]}&#39;, &#39;{$content_array[&#39;content&#39;]}&#39;, &#39;{$content_array[&#39;title&#39;]}&#39;, &#39;&#39;, &#39;publish&#39;, &#39;open&#39;, &#39;open&#39;, &#39;&#39;, &#39;{$content_array[&#39;title&#39;]}&#39;, &#39;&#39;, &#39;&#39;, &#39;{$content_array[&#39;time&#39;]}&#39;, &#39;{$content_array[&#39;time&#39;]}&#39;, &#39;&#39;, 0, &#39;http://iniu.sinaapp.com/?p={$last_id}&#39;, 0, &#39;post&#39;, &#39;&#39;, 0);";

$result = mysql_query($sql,$connect);

$sql ="INSERT INTO `wp_term_relationships` (`object_id`, `term_taxonomy_id`, `term_order`) VALUES({$last_id}, 1, 0);";

$result = mysql_query($sql,$connect);
return $result;
}

function getContent($value){
global $rules; 
preg_match($rules[&#39;title&#39;],$value, $title);

preg_match($rules[&#39;time&#39;],$value, $time);

preg_match($rules[&#39;next&#39;],$value, $next);

preg_match($rules[&#39;content&#39;],$value, $content);

$context = array(
&#39;title&#39; => addslashes($title[1]),
&#39;time&#39; => $time[1],
&#39;next&#39; => addslashes($next[1]),
&#39;content&#39; => addslashes($content[1])
);
return $context;
}


function getLatest(){
 global $connect;
 global $rules; 
 $sql ="SELECT url FROM `articles` ORDER BY id DESC LIMIT 1";
 $result = mysql_query($sql,$connect);
$row=mysql_fetch_row($result);

if($row){
return $row[0]; 
}else{
return $rules[&#39;start&#39;];
}

}


function get($url){
 $ch = curl_init($url) ;
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ;
 curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ;
 $value = curl_exec($ch) ;
curl_close($ch);
 return $value;
}


function _prefilter($output) {
strip_tags($output);
$output=preg_replace("///[Sftv ]*?;[r|n]/","", $output);
$output=preg_replace("/<!--[sS]*?-->/","", $output);
$output=preg_replace("/>[s]+</","><", $output);
$output=preg_replace("/;[s]+/",";", $output);
$output=preg_replace("/[s]+}/","}", $output);
$output=preg_replace("/}[s]+/","}", $output);
$output=preg_replace("/{[s]+/","{", $output);
$output=preg_replace("/([s]){2,}/","$1", $output);
$output=preg_replace("/[s]+=[s]+/","=", $output);
$output=preg_replace("/<br />/","",$output);
$output=preg_replace("/n/","",$output);
$output=preg_replace("/ /","",$output);
return $output;
}


?>
登入後複製

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板