Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4734635
  • 博文数量: 206
  • 博客积分: 5240
  • 博客等级: 大校
  • 技术积分: 3224
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-12 21:40
文章分类

全部博文(206)

文章存档

2013年(13)

2012年(8)

2011年(33)

2010年(152)

我的朋友

分类:

2010-09-12 00:41:15

php
/**
  * baidu 贴吧自动回帖机
  * author: hetal 
  * email : [email]huoqi29@163.com[/email]
  * qq    : 231073376
  * msn   : [email]hetao29@hotmail.com[/email]
  * web   : [url][/url]
  *
  */
class Baidu_Post{
    var 
$proxyPort;
    var 
$proxyHost;
    var 
$cookie;
    
//设置代理 ,ok
    
function setProxy($proxyHost,$proxyPort){
        
$this->proxyPort=$proxyPort;
        
$this->proxyHost=$proxyHost;
    }
    
//初始化baiduID ,ok
    
function init(){
        
$data $this->_getUrl("/u/info_img/2008-08/18/logo.gif");
            
preg_match_all ("/Set-Cookie: (.+?)=(.+?);/"$data,$_matches,PREG_SET_ORDER);
            
$tmp=array();
            for(
$i=0;$i<count($_matches);$i++){
                    
$tmp[]=$_matches[$i][1]."=".$_matches[$i][2];
            }
            
$this->cookie=implode(';',$tmp);
    }
    
//登录
    
function login($user,$pass){
        
$cookie=$this->cookie;
        
$post_data="tpl=&tpl_ok=&skip_ok=&aid=0&need_pay=&need_coin=0&pay_method=0&u=&next_target=&return_method=&more_param=&return_type=&username=$user&password=$pass&Submit=+%B5%C7%C2%BC+";
        
$post_data_le strlen($post_data);
        if(!empty(
$this->proxyPort) and !empty($this->proxyHost)){
            
$post_url="";
            
$post_port=$this->proxyPort;
            
$post_host=$this->proxyHost;
            
$connect="Proxy-Connection";
        }else{
            
$post_url="/?login";
            
$post_port=80;
            
$connect="Connection";
            
$post_host="passport.baidu.com";
        }
        
$data="POST $post_url HTTP/1.1\nAccept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*\nReferer: [url][/url]\nAccept-Language: zh-cn\nContent-Type: application/x-www-form-urlencoded\nAccept-Encoding: gzip, deflate\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Alexa Toolbar)\nHost: passport.baidu.com\nContent-Length: $post_data_le\n$connect: Keep-Alive\nCache-Control: no-cache\nCookie: $cookie\n\n$post_data\n";
        
$sock fsockopen($post_host,$post_port,$errno,$error,3);
        
fputs($sock,$data);
        while(
$rt[]=fgets($sock)){}
        
fclose($sock);
        
$rt=implode("",$rt);
        
$rt=str_replace("\n","",$rt);
        
$rt=str_replace("\t","",$rt);
        
preg_match_all ("/Set-Cookie: (.+?)=(.+?);/"$rt,$_matches,PREG_SET_ORDER);
        
$tmp=array();
        for(
$i=0;$i<count($_matches);$i++){
                
$tmp[]=$_matches[$i][1]."=".$_matches[$i][2];
        }
        
$this->cookie=$cookie.";".implode(';',$tmp);
        if(isset(
$tmp['BDUSS']) and !empty($tmp['BDUSS'])){
            return 
0;
        }else{
            return 
1;
        }
    }
    
//得到最热的500个贴吧
    
function getTop500($url=""){
        
$content=$this->_getUrl($url);
        
$title=array();
        
//preg_match_all ("/\(.+?)\<\/a\>/", $content, $_matches,PREG_SET_ORDER);
        
preg_match_all ("/\(.+?)\<\/a\>/",$content$_matches,PREG_SET_ORDER);
        foreach(
$_matches as $value){
            
$tmp=array();
            
$tmp['url']="".$value[1];
            
$tmp['title']=$value[3];
            
$title[]=$tmp;
        }
        return 
$title;
    
    }
    
//得到每个贴吧对应的第1页的贴子
    
function getTitle($tieba_url){
        
$content=$this->_getUrl($tieba_url);
        
//echo $content;
        
$title=array();
        
preg_match_all ("/\(.+?)\<\/a\>/"$content$_matches,PREG_SET_ORDER);
        
//print_r($_matches);
        
foreach($_matches as $value){
            
$tmp=array();
            
$tmp['url']="?kz=".$value[2];
            
$tmp['title']=$value[4];
            
$title[]=$tmp;
        }
        return 
$title;
    }
    
//回贴
    
function huitie($url,$title1,$title="",$content="",$logo="http://"){
        
$fileds=$this->_getTie($url);
        if(empty(
$fileds)){return -1;}
        
$tmp[]="ct=".$fileds['ct'];
        
$tmp[]="tn=".$fileds['tn'];
        
$tmp[]="word=".urlencode($fileds['word']);
        
$tmp[]="lm=".$fileds['lm'];
        
$tmp[]="z=".$fileds['z'];
        
$tmp[]="sc=".$fileds['sc'];//贴子总数
        
$tmp[]="cm=".$fileds['cm'];
        
$tmp[]="rn=".$fileds['rn'];
        
$tmp[]="bs=".$fileds['bs'];
        
$tmp[]="str2=".$fileds['str2'];
        
$tmp[]="rs3=".$fileds['rs3'];
        
$tmp[]="bu=".urlencode($url);//这是回跳地址 //window.location
        
$tmp[]="ti=".rawurlencode($title);//标题
        
$tmp[]="ti1=".rawurlencode($title1);
        
$tmp[]="co=".rawurlencode($content);
        
$tmp[]="str1=".rawurlencode($logo);
        
$tmp[]="ch1=on";
        
$tmp[]="Submit3=%B7%A2%B1%ED%CC%F9%D7%D3";
        
$cookie=$this->cookie;
        
$post_data =implode("&",$tmp);
        
$post_data_le strlen($post_data);
        if(!empty(
$this->proxyPort) and !empty($this->proxyHost)){
            
$post_url="";
            
$post_port=$this->proxyPort;
            
$connect="Proxy-Connection";
            
$post_host=$this->proxyHost;
        }else{
            
$post_url="/f";
            
$post_port=80;
            
$connect="Connection";
            
$post_host="tiebacommit.baidu.com";
        }
        
$data="POST $post_url HTTP/1.1\nAccept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*\nAccept-Language: zh-cn\nContent-Type: application/x-www-form-urlencoded\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Alexa Toolbar)\nHost: tiebacommit.baidu.com\nContent-Length: $post_data_le\nConnection: Keep-Alive\nCache-Control: no-cache\nCookie: $cookie\n\n$post_data\n";
        
$sock fsockopen($post_host,$post_port,$errno,$error,3);
        
fputs($sock,$data);
        while (!
feof($sock)) {
            
$rt[]=fgets($sock,1024);
        }
        
fclose($sock);
        
$rt=implode("",$rt);
        
$rt=str_replace("\n","",$rt);
        
$rt=str_replace("\t","",$rt);
        
preg_match_all ("/var rsn=(.+?);/"$rt,$_matches,PREG_SET_ORDER);
        return isset(
$_matches[0][1])?$_matches[0][1]:-99;
    }
    function 
_getTie($url="?kz=190275966"){
        
$content=$this->_getUrl($url);
        
$title=array();
        
preg_match_all ("/\/",$content$_matches,PREG_SET_ORDER);
        foreach(
$_matches as $value){
            
$key  =trim($value[1]);
            
$v    =str_replace("\"","",trim($value[2]));
            
$title[$key]=($v);
        }
        
preg_match_all ("/fr_as_js_tr\(\)\{(.+)\}/"$content,$_matches,PREG_SET_ORDER);
        
$title['str2']=$this->_getStr2($_matches[0][1]);
        unset(
$title['pn']);
        unset(
$title['kw']);
        unset(
$title['rs2']);
        
preg_match_all ("/var need_verify=(.+?);/"$content,$_matches,PREG_SET_ORDER);
        if(
$_matches[0][1]==10){
            return 
null;
        }
        return 
$title;
    }
    function 
_getStr2($cmd){
        
$cmd str_replace("var ","",$cmd);
        
$cmd str_replace("Math.","",$cmd);
        
$cmd str_replace("v",'$v',$cmd);
        
$cmd str_replace("i",'$i',$cmd);
        return eval(
$cmd);
    }
    function 
_getUrl($url,$referer="")
    {
        
$purlparse_url($url);
        
$host=$purl['host'];
        if(!empty(
$this->proxyHost) and !empty($this->proxyPort)){
            
$port=$this->proxyPort;
            
$post_host=$this->proxyHost;
        }else{
            
$post_host=$purl['host'];
            if(empty(
$purl['port'])){
                
$port=80;
            }else{
                
$port=$purl['port'];
            }
        }
        
$cookie $this->cookie;
        
$path=$purl['path'];
        
$query=empty($purl['query'])?"":$purl['query'];
        if(!empty(
$proxyhost) and !empty($proxyport)){
            
$content="";
            
$content.="GET $url HTTP/1.0\r\n";
            
$content.="Accept: */*\r\n";
            if(!empty(
$referer))
            
$content.="Referer: $referer\r\n";
            
$content.="Accept-Language: zh-cn\r\n";
            
$content.="Proxy-Connection: Keep-Alive\r\n";
            
$content.="User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\r\n";
            
$content.="Host: $host\r\n";
            
$content.="Pragma: no-cache\r\n";
            
$content.="Cookie: $cookie\r\n";
            
$content.="\r\n\r\n";
        }else{
            
$content="";
            if(!empty(
$query))
            
$content.="GET $path?$query HTTP/1.0\r\n";
            else
            
$content.="GET $path HTTP/1.0\r\n";
            
$content.="Accept: */*\r\n";
            if(!empty(
$referer))
            
$content.="Referer: $referer\r\n";
            
$content.="Accept-Language: zh-cn\r\n";
            
$content.="Connection: Keep-Alive\r\n";
            
$content.="User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\r\n";
            
$content.="Host: $host\r\n";
            
$content.="Pragma: no-cache\r\n";
            
$content.="Cookie: $cookie\r\n";
            
$content.="\r\n\r\n";
        }
        
$sock=fsockopen ($post_host,$port,$errno$errstr3);
        if(
$sock){
            
fwrite($sock,$content);
        }else{
            return 
null;
        }
        while(
$r[]=fgets($sock)){};
        
fclose($sock);
        return 
implode("\n",$r);
    }


}

$baidu = new Baidu_Post;
$baidu->init();
$baidu->login("user","pass");
$baidu->huitie($url,$title,$my_title,$my_content,$my_logo);
$baidu->huitie("?kz=199435887","【懂我】为什么排名上不去了 ","我的标题","我的内容","");
?> 
阅读(685) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~