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

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

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来