duanli5662 2015-11-09 15:34
浏览 70
已采纳

自动登录Stackoverflow

I get quite annoyed by logging in to SO sometimes and i'd like to do it by just calling a PHP file. I tried doing so by sending a post request like this:

<?php
$url = 'https://stackoverflow.com/users/login?ssrc=head&amp;returnurl=http%3a%2f%2fstackoverflow.com%2f';
$data = array('email' => 'mymail@gmail.com', 'password' => 'mypasswort');

$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded
",
        'method'  => 'POST',
        'content' => http_build_query($data),
    ),
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);

var_dump($result);
?>

I also tried including more values like ssrc and fkey but i just shows the SO mainpage after submitting. It even stays at localhost (or wherever the script is running). If i enter a wrong password it marks it as incorrect - so it has to work at lest in some way (the data verification)...

  • 写回答

1条回答 默认 最新

  • dongyan7876 2015-11-09 15:38
    关注

    You must send post data:

    isSignup:false
    isLogin:true
    isPassword:false
    isAddLogin:false
    hasCaptcha:false
    fkey:922b6dc5a5a375283c44e298246d7763
    ssrc:head
    email:asd
    password:
    submitbutton:Log in
    oauthversion:
    oauthserver:
    openidusername:
    openididentifier:
    

    You should send first GET request and parse fkey from result page (this is CSRF protection). fkey you should send with POST request with post data which I show you.

    Simple example:

    <?php
    
    $url = 'https://stackoverflow.com/users/login?ssrc=head';
    $data = array(
        'email' => 'mail@email.z',
        'password' => 'pass',
        'isSignup' => false,
        'isLogin' => true,
        'isPassword' => false,
        'isAddLogin' => false,
        'hasCaptcha' => false,
        'fkey' => '',
        'ssrc' => 'head',
        'submitbutton' => 'Log in',
        'oauthversion' => '',
        'oauthserver' => '',
        'openidusername' => '',
        'openididentifier' => ''
    );
    
    $options = array(
        'http' => array(
            'header' => "Content-type: application/x-www-form-urlencoded
    ",
            'method' => 'GET',
        ),
    );
    $context = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    
    preg_match('~name\=\"fkey\"\svalue=\"([\w]+)\"~', $result, $matches);
    
    $data['fkey'] = $matches[1];
    
    $options = array(
        'http' => array(
            'header' => "Content-type: application/x-www-form-urlencoded
    ",
            'method' => 'POST',
            'content' => http_build_query($data),
        ),
    );
    $context = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    
    var_dump($result);
    

    But I recommend for you use SO API and you willn't have any problems

    If you will have problem, write

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示