上篇博文博主提到要做一个百度贴吧抢楼器,并且已经实现了获取指定帖子楼层的功能。这篇继续~
要达到抢楼的目的,最最关键的一步就是实现自动回复。
下面就给出利用PHP在百度贴吧回帖的代码
- <?php
- //注:本代码来源网络,原作者不详
- $content=“”;//你要回复的内容,如要换行请用\n,如要用图片请按照这个格式 #(pic,2b8806e93901213f3c8b371655e736d12d2e95dc,442,614) 表情是这个格式 #(滑稽)
- $bduss=“”; //你的bduss
- $tid=“”;//你要回复贴子的tid
- header(“content-Type: text/html; charset=Utf-8”);
- function curl_post($pda,$url){
- $header = array (“Content-Type: application/x-www-form-urlencoded”);
- $data=implode(“&”, $pda).“&sign=”.md5(implode(“”, $pda).“tiebaclient!!!”);
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
- curl_setopt($ch, CURLOPT_TIMEOUT, 30);
- $re = json_decode(curl_exec($ch));
- curl_close($ch);
- return $re;
- }
- function rand_int($l) { //生成指定位数的随机数
- $int = null;
- for ($e=0; $e < $l; $e++) {
- $int .= mt_rand(0,9);
- }
- return $int;
- }
- function getPage(){
- global $bduss,$tid;
- $pd=array(
- “BDUSS=”.$bduss,
- “_client_id=wappc_136” . rand_int(10) . “_” . rand_int(3),
- “_client_type=”.mt_rand(1,4),
- “_client_version=5.0.0”,
- “_phone_imei=”.md5(rand_int(16)),
- “back=0”,
- “kz=”.$tid,
- “net_type=3”,
- “pn=1”,
- “rn=2”,
- “with_floor=1”,
- );
- $gd=curl_post($pd,’http://c.tieba.baidu.com/c/f/pb/page’);
- if($gd->error_code!=0)exit(‘发生错误:’.$gd->error_msg.'<br>’);
- $tbs=$gd->anti->tbs;
- $fid=$gd->forum->id;
- $kw=$gd->forum->name;
- $title=$gd->thread->title;
- return replyPost($kw,$tbs,$fid,$title);
- }
- function replyPost($kw,$tbs,$fid,$title){
- global $bduss,$tid,$content;
- $pd=array(
- “BDUSS=”.$bduss,
- “_client_id=wappc_136” . rand_int(10) . “_” . rand_int(3),
- “_client_type=”.mt_rand(1,4), //设备标识,1-iPhone,2-Android,3-Windows Phone,4-Windows 8
- “_client_version=5.0.0”,
- “_phone_imei=”.md5(rand_int(16)),
- ‘anonymous=0’,
- ‘content=’.$content,
- ‘fid=’.$fid,
- //’is_ad=0′,
- “kw=”.$kw,
- “net_type=3”,
- ‘tbs=’.$tbs,
- ‘tid=’.$tid,
- ‘title=’
- );
- $gd=curl_post($pd,’http://c.tieba.baidu.com/c/c/post/add’);
- if($gd->error_code!=0)exit($kw.’吧|’.$title.’|发生错误:’.$gd->error_msg.'<br>’);
- echo $kw.’吧|’.$title.’|回复成功<br>’;
- }
- getPage();
- ?>
注:BDUSS可在这里获取☞http://tool.mkblog.cn/BDUSS/
来源:https://mkblog.cn/223/