继上一篇文章 《模拟登录 pixiv.net 后续》,老接口已经在 html 上消失了,那么只能按新版接口来惹
直接上代码,添加了登录错误的异常 =w= 同时不再需要 sunra/php-simple-html-dom-parse
可以看到 p 站添加了 Google reCAPTCHA,但是目前没有碰到验证码。
PS: is_cookie_ok 没有改动,依旧按原来的使用。
PSS: nodejs 版 pixiv-cookie

define('BEFORE_LOGIN_COOKIE', __DIR__ . '/BEFORE_LOGIN_COOKIE.txt'); //登录页面的cookie
define('AFTER_LOGIN_COOKIE', __DIR__ . '/AFTER_LOGIN_COOKIE.txt'); //登录成功后获取到的cookie
define('USER_AGENT', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.59 Safari/537.36');

login_pixiv('YOUR_PIXIVID', 'YOUR_PWD');
var_dump(is_cookie_ok(AFTER_LOGIN_COOKIE));

function login_pixiv($username, $password)
{
    //获取登陆页cookie
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://www.pixiv.net');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_COOKIEJAR, BEFORE_LOGIN_COOKIE);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
    curl_setopt($ch, CURLOPT_USERAGENT, USER_AGENT);

    $login_html = curl_exec($ch);
    curl_close($ch);

    preg_match('/pixiv\.context\.token(.*)=(.*?)(\"|\')(.*?)(\"|\')/', $login_html, $matches);
    if (!isset($matches[4])) {
        throw new Exception('没有获取到token');
    }

    $data['pixiv_id'] = $username;
    $data['password'] = $password;
    $data['captcha'] = '';
    $data['g_recaptcha_response'] = '';
    $data['post_key'] = $matches[4];
    $data['source'] = 'pc';
    $data['ref'] = 'wwwtop_accounts_index';
    $data['return_to'] = 'http://www.pixiv.net/';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://accounts.pixiv.net/api/login?lang=zh');
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Origin: https://accounts.pixiv.net',
        'User-Agent: ' . USER_AGENT,
        'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
        'Referer: https://accounts.pixiv.net/login?lang=zh&source=pc&view_type=page&ref=wwwtop_accounts_index',
        'X-Requested-With: XMLHttpRequest',
    ));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_COOKIEFILE, BEFORE_LOGIN_COOKIE);
    curl_setopt($ch, CURLOPT_COOKIEJAR, AFTER_LOGIN_COOKIE);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $result = curl_exec($ch);
    if ($result === false) {
        throw new Exception(curl_error($ch));
    } else {
        $result = json_decode($result, true);
        if ($result['error']) {
            throw new Exception($result['message']);
        } else {
            if (!$result['body']['success']) {
                throw new Exception(json_encode($result['body']));
            }
        }
    }
    curl_close($ch);
}

--再怎么看都没有啦|´・ω・)ノ--