Как авторизоваться через cURL на удаленном сайте и перейти на него будучи авторизованным?

Пытаюсь пройти авторизацию но ничего не получается. На сайте как я понял двойная авторизация, сначала указываю метод авторизации, потом пароль на другой странице.

Вот что отправляется на сервер:

Авторизация:

1. https://my.tiu.ru/remote/auth/phone_email_step?locale=ru
{"phone_email":"[email protected]","pow_complexity":"18:web.E5Wiaw.2b7O2wAfRAK63vuQO7XNRJVfvAs","pow_prefix":"QPUT82H1QDHCSHV2FHJQI1ETLY0UT4TWMENPFK64WLNGYWMABTXXC6GQ5Y66KWK7.E5Wiaw.vdnsW9dzomQD5iFgEaN944Cy_is","pow_nonce":"00000179bf6221a1d873121c6ee4b149"}

2. https://my.tiu.ru/remote/auth/enter_password_step?locale=ru
{"phone_email":"[email protected]","password":"test@","next_url":"/cabinet/user/orders","pow_complexity":"18:web.E5WjAw.zTRC4H4qRdJsld6wYIXs-aeJp_k","pow_prefix":"UCAE19F2ERTM7CG1XOV35DA8ZXOL4CQBVC27ET2UV3JD67WRTB5JJ9H1ZLGVGBZG.E5WjAw.Dt1t3iHaQMq0PsvCSwKpAbaekZg","pow_nonce":"00000179bf6930d973d218443f5d8f5b"}

Т.е. к запросу прикрепляются еще дополнительные параметры, при выборе способа авторизации + 3 параметра, при вводе пароля + 4 параметра. Если я все верно понял.

Вот что я делаю с помощью cURL:

<?php

set_time_limit(0);

ini_set("display_errors", "On");

define('USER', '[email protected]');
define('PASS', 'test@');

Class Tiu
{
private $ch;
private $auth_cookie_file;
private $users = [];

function __construct($auth_cookie_file)
{
    $this->auth_cookie_file = $auth_cookie_file;
    $this->ch = curl_init();
}

// Авторизация

public function auth($login, $password)
{
    //$this->request('POST', 'https://my.tiu.ru/remote/auth/enter_password_step?locale=ru', [
    $this->request('POST', 'https://my.tiu.ru/sign-in/form?next=', [
        'next' => '/cabinet/user/orders',
        'phone_email' => $login,
        'password' => $password,
        'rememberMe' => 'true',
        'submit' => ''
    ]);
}

// Проверка авторизации

public function checkAuth()
{
    $response = @json_decode($this->request('GET', 'https://my.tiu.ru/cabinet/user/orders')['body'], true);

    return is_array($response) && isset($response['id']);
}

// Общий метод для отправки запросов через curl

private function request($type, $url, $data = [], $headers = [], $curlopts = [])
{
    $default_headers = [
        'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Encoding:gzip, deflate, lzma',
        'Accept-Language:ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4',
        'Upgrade-Insecure-Requests:1',
        'User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36 OPR/42.0.2393.517'
    ];

    $default_curlopts = [
        //CURLOPT_INTERFACE => '46.254.16.170',https://my.tiu.ru/sign-in/form?next=
        //CURLOPT_URL => (strpos($url, 'https://') === 0 ? '' : 'https://my.tiu.ru/remote/auth/enter_password_step?locale=ru') . $url . ($type == 'GET' && count($data) > 0 ? '?' . http_build_query($data) : ''),
        CURLOPT_URL => (strpos($url, 'https://') === 0 ? '' : 'https://my.tiu.ru/sign-in/form?next=') . $url . ($type == 'POST' && count($data) > 0 ? '?' . http_build_query($data) : ''),
        CURLOPT_HTTPHEADER => array_merge($default_headers, $headers),
        CURLOPT_FOLLOWLOCATION => false,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HEADER => true,
        CURLOPT_ENCODING => 'gzip',
        CURLOPT_COOKIEFILE => $this->auth_cookie_file,
        CURLOPT_COOKIEJAR => $this->auth_cookie_file,
        CURLOPT_FOLLOWLOCATION => true
    ];

    if ($type == 'POST') {
        $default_curlopts[CURLOPT_POST] = true;
        $default_curlopts[CURLOPT_POSTFIELDS] = http_build_query($data);
    }

    curl_setopt_array($this->ch, array_replace($default_curlopts, $curlopts));

    $response = (string)curl_exec($this->ch);

    $result = [
        'http_code' => curl_getinfo($this->ch, CURLINFO_HTTP_CODE),
        'raw_response' => $response,
        'header_size' => curl_getinfo($this->ch, CURLINFO_HEADER_SIZE)
    ];

    $result['header'] = mb_substr($response, 0, $result['header_size'], 'UTF-8');
    $result['headers'] = array_filter(array_map('trim', explode("\n", $result['header'])));
    $result['body'] = mb_substr($response, $result['header_size'], null, 'UTF-8');

    return $result;
var_dump($result);

}
}


// Проверка авторизации. Если уже авторизован, то идет дальше, нет - авторизуется по логину и паролю
$tiu = new Tiu(__DIR__ . '/auth.cookie');


if (!$tiu->checkAuth()) {
$tiu->auth(USER, PASS);

if (!$tiu->checkAuth()) {
    die('Can\'t auth');
}
var_dump($tiu);

}

Содержание файла auth.cookie

# Netscape HTTP Cookie File
# http://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

#HttpOnly_.tiu.ru   TRUE    /   TRUE    1653991682  evoauth w45ba41643da3497b8a80bd6fd17de7ee
.tiu.ru TRUE    /   FALSE   1625047683  cid 302199160435600002842890569314094394472
.tiu.ru TRUE    /   FALSE   0   user_tracker    88daf938c0e6515d0561a3d407e9313efd4c6546|91.218.231.18|2021-05-31
.tiu.ru TRUE    /   TRUE    1653905284  csrf_token  b607e198f6f448c1bac0d4ea213f1e7f
#HttpOnly_.tiu.ru   TRUE    /   TRUE    1653991683  auth    7bb28d92619fc8bd8cb6c0aa3dd7d6e1b253b6b0

Авторизоваться не получается, где ошибаюсь?

Как исправить авторизацию?


Ответы (0 шт):