SeriousCool 发表于 2009-08-21 23:40

Discuz发帖机PHP版

写这个的目的不是为了漫天去发广告,所以没考虑验证码和验证问答。

<?
error_reporting("7");
$url = "http://www.*********.com/logging.php";
$headers = get_headers($url);
preg_match("/Set-Cookie: (.+?);/", $headers[6], $harr);
$cookie = $harr[1];
$data = array(
                'action'=>'login',
                'loginsubmit'=>'yes',
                'username'=>'*********',
                'password'=>'*********',
                'cookietime'=>'2592000'
                );
$data = http_build_query($data);
$opts = array(
                'http'=>array(
                        'method'=>'POST',
                        'header'=>"Content-type: application/x-www-form-urlencoded\r\n".
                        "$cookie\r\n"."Content-Length:".strlen($data)."\r\n",
                        'content'=>$data
                        )
                );
$context = stream_context_create($opts);

$fp = fopen($url, 'r', false, $context);
while(!feof($fp)){
      $html .= fgets($fp);
}
preg_match("/formhash=(.{8})\"\>/", $html, $array);
$formhash = $array['1'];
echo "cookie:$cookie\nformhash:$formhash\n";
?>


上面是登录部分,取得cookie和formhash。

需要注意的是formhash,这个东西是会变的,唯一一个肯定会受到影响的因素就是时间,如果其他因素(用户名,UID,密码,AUTHKEY,等等)都不变的话, formhash会每隔100000000秒改变一次(1157天)。


发帖部分还没有写。

[ 本帖最后由 SeriousCool 于 2009-8-21 23:45 编辑 ]

HonestQiao 发表于 2009-08-22 10:36

不错,好好学习。

我以前有一个版本发过,但是怕人乱用,隐藏了代码。

目前手头的这个版本,呵呵,采发回短消息都支持。

benjiam 发表于 2009-08-22 12:55

验证码 是关键。

StartNow 发表于 2009-08-24 21:41

原帖由 HonestQiao 于 2009-8-22 10:36 发表 http://bbs3.chinaunix.net/images/common/back.gif
不错,好好学习。

我以前有一个版本发过,但是怕人乱用,隐藏了代码。

目前手头的这个版本,呵呵,采发回短消息都支持。

验证码问题是如何解决的?

aaaaa5aa 发表于 2009-08-25 01:40

验证码问题目前都是取色吧

网鬼 发表于 2009-08-25 14:33

以前用curl写过一个,也是想不到好的解决验证码的问题

SeriousCool 发表于 2010-04-07 09:45

本帖最后由 SeriousCool 于 2012-12-24 16:01 编辑

突然发现加精了,把所有代码都帖出来吧,不然太对不起观众了。
本人不是专业的程序员,偶尔写些小程序玩玩,代码很不规范,见笑了。
原文地址:
<?
// 登录
function dz_login($url_login, $username, $password){
        $argv_login = array(
                        'action'=>'login',
                        'loginsubmit'=>'yes',
                        'username'=>$username,
                        'password'=>$password,
                        'cookietime'=>'2592000'
                        );

        $ch_login = curl_init();
        curl_setopt($ch_login, CURLOPT_URL, $url_login);
        curl_setopt($ch_login, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch_login, CURLOPT_POST, TRUE);
        curl_setopt($ch_login, CURLOPT_POSTFIELDS, $argv_login);
        curl_setopt($ch_login, CURLOPT_HEADER, TRUE);

        $html_login = curl_exec($ch_login);
        if($html_login === FALSE){
                $error_login = curl_error($ch_login);
        }

        preg_match("/Set-Cookie: (.+?);/", $html_login, $harr);
        $cookie = $harr;

        return $cookie;
}

// 获得formhash
function getformhash($cookie){
        global $message, $fid, $tid;
        $html_formhash = post_reply($message, $fid, $tid);
        preg_match("/formhash=(.{8})\"\>/", $html_formhash, $array);
        $formhash = $array['1'];
        return $formhash;
}

// 发主题
function post_subject($subject, $message, $fid){
        global $cookie, $url_post, $formhash;
        $argv_post_reply = array(
                'action'=>'newthread',
                'fid'=>$fid,
                'formhash'=>$formhash,
                'subject'=>$subject,
                'typeid'=>'1',
                'message'=>$message,
                'topicsubmit'=>'yes',
                'usesig'=>'1'
        );

        $ch_post_reply = curl_init();
        curl_setopt($ch_post_reply, CURLOPT_URL, $url_post);
        curl_setopt($ch_post_reply, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch_post_reply, CURLOPT_POST, TRUE);
        curl_setopt($ch_post_reply, CURLOPT_POSTFIELDS, $argv_post_reply);
        curl_setopt($ch_post_reply, CURLOPT_HEADER, TRUE);
        curl_setopt($ch_post_reply, CURLOPT_COOKIE, $cookie);
        curl_setopt($ch_post_reply, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        $html_reply = curl_exec($ch_post_reply);
                if($html_reply === FALSE){
                $error_reply = curl_errno($ch_post_reply)
                        .curl_error($ch_post_reply);
                return $error_reply;
        }else{
                return $html_reply;
        }
}

// 发回复
function post_reply($message, $fid, $tid){
        global $cookie, $url_post, $formhash;
        $argv_post_reply = array(
                'action'=>'reply',
                'fid'=>$fid,
                'tid'=>$tid,
                'formhash'=>$formhash,
                'message'=>$message,
                'replysubmit'=>'yes',
                'usesig'=>'1'
        );

        $ch_post_reply = curl_init();
        curl_setopt($ch_post_reply, CURLOPT_URL, $url_post);
        curl_setopt($ch_post_reply, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch_post_reply, CURLOPT_POST, TRUE);
        curl_setopt($ch_post_reply, CURLOPT_POSTFIELDS, $argv_post_reply);
        curl_setopt($ch_post_reply, CURLOPT_HEADER, TRUE);
        curl_setopt($ch_post_reply, CURLOPT_COOKIE, $cookie);
        curl_setopt($ch_post_reply, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        $html_reply = curl_exec($ch_post_reply);

        if($html_reply === FALSE){
                $error_reply = curl_errno($ch_post_reply)
                        .curl_error($ch_post_reply);
                return $error_reply;
        }else{
                return $html_reply;
        }
}
?>

zhaiduo 发表于 2010-04-09 12:46

验证码 是关键。
benjiam 发表于 2009-08-22 12:55 http://bbs.chinaunix.net/images/common/back.gif


验证码测试是没问题,但是效率不高,而且不是100%有效,特别是复杂的验证码
http://www.zhaiduo.com/captcha/

shang2010 发表于 2014-06-28 12:39

恩学习一下实用的功能
页: [1]
查看完整版本: Discuz发帖机PHP版