百度贴吧发帖机、盖楼机程序(PHP版)

不说废话,翠花,上代码

 
[php]
<?php
class httpconnector {
/**Curl类
*
*/
private curl; /**cookie字符串 */ privatecookie;
private read_cookie=false; /**get方式下载网页内容 *@paramurl
*@return web conntent
*/
function __construct(){
handle = fopen(‘./cookie.txt’, ‘r’);content=fgets(handle, 1024); if(content!=""){
this->cookie=content;
this->read_cookie=true; } fclose(handle);;

}
public function get(url) {this->curl = curl_init();

curl_setopt(this->curl, CURLOPT_URL,url);

// 设置header
curl_setopt(this->curl, CURLOPT_HEADER, 1); curl_setopt(this->curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt(this->curl, CURLOPT_COOKIE,this->cookie);//设置cookie 以分号分隔

// 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。
curl_setopt(this->curl, CURLOPT_RETURNTRANSFER, 1); // 运行cURL,请求网页data = curl_exec(this->curl); // 关闭URL请求 curl_close(this->curl);
// 找到cookie 放入cookiestring
preg_match_all("/Set-Cookie:(.*?);/", data,match, PREG_SET_ORDER);
foreach (match asr) {
if (this->cookie != ”) {this->cookie = this->cookie . ‘;’; } if (isset(r[1])) {
this->cookie .= trim(str_replace(“rn”, “”,r[1]));
}
}

handle = fopen(‘./cookie.txt’, ‘w+’); fwrite(handle,this->cookie); fclose(handle);;

return data; } /**POST方式下载网页内容 *@paramurl
*@param params post的信息串 *@return web conntent */ public function post(url, params) {this->curl = curl_init();

curl_setopt(this->curl, CURLOPT_URL,url);

// 设置header
curl_setopt(this->curl, CURLOPT_HEADER, 1); curl_setopt(this->curl, CURLOPT_COOKIE, this->cookie); curl_setopt(this->curl, CURLOPT_POST, 1);
curl_setopt(this->curl, CURLOPT_USERAGENT, “Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)”); curl_setopt(this->curl, CURLOPT_POSTFIELDS, params); // 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。 curl_setopt(this->curl, CURLOPT_RETURNTRANSFER, 1);

// 运行cURL,请求网页
data = curl_exec(this->curl);

// 关闭URL请求
curl_close(this->curl); // 找到cookie 放入cookiestring preg_match_all(“/Set-Cookie:(.*?);/”,data, match, PREG_SET_ORDER); foreach (match as r) { if (this->cookie != ”) {
this->cookie =this->cookie . ‘;’;
}
if (isset(r[1])) {this->cookie .= trim(str_replace("rn", "", r[1])); } }handle = fopen(‘./cookie.txt’, ‘w+’);
fwrite(handle,this->cookie);

fclose(handle);; returndata;

}
}
class tieba{
/*抓取编码GBK,抓取贴吧名也为GBK
*回复编码utf-8,登录也用utf-8
*/
private http; function __construct(){this->http=new httpconnector;

}
function login(username,pwd){
data=this->http->post(‘http://wappass.baidu.com/passport/’ , "login_username=".username.”&login_loginpass=”.pwd."&login=yes&aa=登录&can_input=0");

if(strpos(data,”密码错误”)>0){ return 0; }else if(strpos(data,"验证码")>0) {
return 1;
}else if(strpos(data,”账户不存在”)>0){ return 2; }else { return 3; } } function send(tiebaname,title,content){
url=”http://wapp.baidu.com/f/?kw=”.tiebaname;
data=this->http->get(url);data=explode(‘<div class="d h">’,data);data=explode(‘</form>’,data[1]);pos=strpos("action ",data[0]);action=substr(data[0],pos+15);
action=explode(‘”‘,action);
action=action[0];
data=explode(“data[0]);
num=count(data);
postdata=””; for(i=3;i<num;i++){pos1=strpos(data[i],"name");
name=substr(data[i],pos1+6);
name=explode(‘”‘,name);
name=name[0];
pos1=strpos(data[i],”value”);value=substr(data[i],pos1+7);value=explode(‘"’,value);value=value[0];postdata.=name.”=”.value."&";
if(name==’sub1′) break; }postdata.="ti=".title.”&co= “.content;
posturl=”http://wapp.baidu.com/”.action;
data=this->http->post(posturl,postdata);
data=this->http->get(url); /**注意置顶帖带来的误差 */tid=explode(‘<div class="i">’,data);num=count(tid); for(i=1;i<num;i++){ if(strpos(tid[i],’‘)>0){ }else{tid=explode("m?kz=",tid[i]);
tid=explode(“&”,tid[1]);
tid=tid[0];
break;

}

}

return tid; } function logout(){url="http://wapp.baidu.com";
data=this->http->get(url);data=explode("退出</a>",data);data=explode(‘href="’,data[0]);count=count(data);data=explode(‘"’,data[count-1]);
logouturl=data[0];
ret=this->http->get(logouturl); returnret;

}
function replay(tid,content){
url=”http://wapp.baidu.com/f/?kz=”.tid;

data=this->http->get(url); echodata;
data=explode(‘

‘,data);
data=explode(‘

‘,data[1]);
pos=strpos(“action “,data[0]);
action=substr(data[0],pos+15);action=explode(‘"’,action);action=action[0];data=explode("<input",data[0]);num=count(data);postdata="";
for(i=3;i<num;i++){
pos1=strpos(data[i],”name”);name=substr(data[i],pos1+6);name=explode(‘"’,name);name=name[0];pos1=strpos(data[i],"value");
value=substr(data[i],pos1+7);
value=explode(‘”‘,value);
value=value[0];
postdata.=name."=".value.”&”; if(name==’sub1′)
break;
}
postdata.=”co=”.content;
url=”http://wapp.baidu.com/”.action;
echo url;data=this->http->post(url,postdata); echodata;

}
/*
*抓取的贴吧名和返回的结果都是GBK
*/
function crul(tiebaname){tiebaname= iconv("UTF-8","GBK", tiebaname);url="http://tieba.baidu.com/f?kw=".tiebaname;content=this->http->get(url);
out=explode(“

“,content);
num=count(out)-7;
result=array(); for(i=1;i<num+1;i++){tli=explode("</td>",out[i]);
pos1 = (int)strpos(tli[0], ‘<td nowrap>’);
clickcount=substr(tli[0],pos1+11);replaynum=this->getNum(tli[1]);
author=trim(strip_tags(tli[3]));
replay=this->getChinese(tli[4]);href=tli[2];pos1 = (int)strpos(href, ‘”_blank”‘);pos2 = (int)strpos(href, ‘‘);title=substr(href,pos1+9,pos2-pos1-5);
title=strip_tags(title);
pos1 = (int)strpos(href, ‘/p’);
tid=substr(href,pos1+3,10); if(replay=="")
replay=author;
result[]=array(“clickcount”=>clickcount ,"replaynum"=>replaynum,”title”=>title,"author"=>author,”tid”=>tid,"replay"=>replay); } returnresult;

}
function getChinese(string) {tmpstr = ”;
arr = array(1,2,3,4,5,6,7,8,9,0);strlen = strlen(string); for(i=0; i<strlen; i++) {str=substr(string,i, 1);
str1=trim(str);
if( ord(str)>0xA0 ){tmpstr.= substr(string,i, 2);
i =i+1;
}

}
return tmpstr; } function getChineseNum(string) {
tmpstr = ”;arr = array(1,2,3,4,5,6,7,8,9,0);
strlen = strlen(string);
for(i=0;i<strlen;i++) {
str=substr(string, i, 1);str1=trim(str); if( ord(str)>0xA0 ){
tmpstr.= substr(string, i, 2);i = i+1; } if(is_numeric(str1)){
tmpstr.=str1;
}

}
return tmpstr; } function getNum(string){
tmpstr = ”;arr = array(1,2,3,4,5,6,7,8,9,0);
strlen = strlen(string);
for(i=0;i<strlen;i++) {
str=substr(string, i, 1);str1=trim(str); if(is_numeric(str1)){
tmpstr.=str1;
}
}
return $tmpstr;
}

}

?>
[/php]

Leave a Reply

Time limit is exhausted. Please reload CAPTCHA.

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据