<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>轻松签到</title>
<link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<form method="post">
<div class="form-group">
<label for="exampleInputEmail1">输入贴吧的COOKIE:</label>
<input type="text" name="cookie" class="form-control" id="exampleInputEmail1" >
</div>
<button type="submit" class="btn btn-primary btn-block">一键签到</button>
</form>
</body>
</html>
<?php
/*header("content-Type: text/html; charset=gbk");*/
//设置时区
set_time_limit(0);
date_default_timezone_set("PRC");
if(!isset($_POST['cookie'])){
exit();
}
//声明全局变量
global $cookie;
//赋值给cookie
$cookie = $_POST['cookie'];
const URL = 'http://tieba.baidu.com/f/like/mylike';
$result = iconv('gbk','utf-8',_grab(URL,'',$cookie));
/*echo $result;*/
//所有ilike贴吧数组
$tiebaData = getTiebaData($result);
//如果cookie是错的,结束
if(!$tiebaData){
echo '<p>Cookie错误,程序停止!</p>';
ob_flush();
flush();
}else{
echo '<p>登陆成功!u like:'.count($tiebaData).'。签到开始!</p>';
ob_flush();
flush();
}
//遍历所有贴吧,签到
foreach ($tiebaData as $value) {
//签到之后返回的数组
$resultData = json_decode(_daily($value),true);
//如果签到失败
if($resultData['no']){
echo '<p>'.$value.'吧:'.$resultData['error'].'</p>';
}else{
echo '<p>'.$value.'吧:'.'签到成功'.'</p>';
}
ob_flush();
flush();
sleep(3);
}
echo '<p>执行完毕</p>';
//签到
function _daily($keyword){
$url = 'https://tieba.baidu.com/sign/add';
$postData = array(
'ie'=>'utf-8',
'kw'=>$keyword
);
return _grab($url,$postData,$GLOBALS['cookie'],'http://tieba.baidu.com/i/i/forum','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36');
}
/**
* 模拟访问,可get可post
* @param $curl
* @param $postInfo post数组/字符串,不填默认没有post
* @param $cookie cookie模拟
* @param $referer 来路模拟
* @param $userAgent 页数
* @param $userAgent 环境模拟
* @return $content 抓取的内容
*/
function _grab($curl,$postInfo='',$cookie='',$referer='',$userAgent=''){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $curl);
//不输出头
curl_setopt($ch, CURLOPT_HEADER, 0);
//以字符串返回获取的信息,不直接输出
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//如果是https链接,不验证证书
if(preg_match('/https/i', $curl)){
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
}
//POST
if($postInfo){
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$postInfo);
}
//加入cookie
if($cookie){
curl_setopt($ch,CURLOPT_COOKIE,$cookie);
}
//模拟来路
if($referer){
curl_setopt($ch, CURLOPT_REFERER, $referer);
}
//模拟环境
if($userAgent){
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
}
//执行
$content = curl_exec($ch);
//错误处理
if ($content === false) {
return "网络请求出错: " . curl_error($ch);
exit();
}
return $content;
}
//通过内容得到所有ilike贴吧
function getTiebaData($result){
//匹配贴吧名的正则
$preg = '/title\=\"(.+?)\"\>(?:\\1)\<\/a\>/';
//贴吧数组
$tiebaData = [];
//如果有多页
if(preg_match('/\&pn\=(\d+)\"\>尾页\<\/a\>/', $result ,$data)){
//总页数
$pageCount = $data[1];
//遍历所有页面
for($x=1;$x<=$pageCount;$x++){
$pageUrl = URL.'?&pn='.$x;
$content = _grab($pageUrl,'',$GLOBALS['cookie']);
//当前页内容
$content = iconv('gbk','utf-8',$content);
//匹配吧名
if(preg_match_all($preg,$content,$data)){
foreach ($data[1] as $value) {
$tiebaData[] = $value;
}
}
}
}
//如果只有一页
else{
if(preg_match_all($preg,$result,$data)){
foreach ($data[1] as $value) {
$tiebaData[] = $value;
}
}else{
return $tiebaData;
}
}
return $tiebaData;
}