M
M
mapleart2018-07-19 18:01:22
PHP
mapleart, 2018-07-19 18:01:22

How to log in to your mts account using CURL?

Hello. You need to use PHP to log in to your MTS account.
I make a request to the site https://lk.ssl.mts.ru/ and in response I get this
5b50a43506f7a964605248.png
. I try to get the parameters from the form and send a post request. As a result, I receive such an answer
5b50a7b26d11e172475306.png
Who can tell what it is?
Here is a snippet of curl code

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$phone = '915111111';
$pass = '11111';
$aHeaders = array(
    'Connection: keep-alive',
    'Upgrade-Insecure-Requests: 1',

    'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
    'Accept-Encoding: gzip, deflate, sdch, br',
    'Accept-Language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // следовать за редиректами
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/' . $phone . '-cookie.txt'); // сохранять куки в файл
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . '/' . $phone . '-cookie.txt');
curl_setopt($ch, CURLOPT_USERAGENT, ' Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36');


function CURL($url, $aPost=array(), $aHeaders=array()){
    global $ch;
    curl_setopt($ch, CURLOPT_URL,  $url);
    if(is_array($aPost) && count($aPost)){
        curl_setopt($ch, CURLOPT_POST, 1); // использовать данные в post
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($aPost));
    }
    if(count($aHeaders)){
        curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeaders);
    }
    $html = curl_exec($ch);
    if(curl_exec($ch) === false) {
        $error = 'Ошибка curl : ' . curl_error($ch);
        print_r( $error);
        return false;
    }
    else {
        return $html;
    }
}


function ParseForm($htmlForm)
{
    $valueRegExp = '/value\\s*=\\s*("[^"]*"|\'[^\']*\'|[\\w\\-\\/\\\\]+)/i';
    $inputRegExp = '/<input[^>]+name\\s*=\\s*("[^"]*"|\'[^\']*\'|[\\w\\-\\/\\\\]+)[^>]*>|<select[^>]+name\\s*=\\s*("[^"]*"|\'[^\']*\'|[\\w\\-\\/\\\\]+)[^>]*>[\\s\\S]*?<\\/select>/i';
    $returnValue = preg_match_all($inputRegExp, $htmlForm, $matches);
    if (!count($matches)) {
        return false;
    }

    $aResult = array();
    foreach ($matches[0] as $key => $sStr) {
        $value = null;
        $name = '';
        $nameInput = $matches[1][$key];
        $selInput = $matches[2][$key];
        if ($nameInput) {
            if (preg_match('/type\s*=\s*[\'"]?button[\'"]?/i', $sStr, $matchesButton)) {
                $value = 'undefined';
            } else if (preg_match('/type\s*=\s*[\'"]?checkbox[\'"]?/i', $sStr, $matchesCheckbox)) {
                if (preg_match('/[^\w\-]checked[^\w\-]/i', $sStr, $matchesCheckbox)) {
                    preg_match($valueRegExp, $sStr, $matchesValue);
                    $value = isset($matchesValue[1]) ? $matchesValue[1] : 'on';
                }
            } else {
                preg_match($valueRegExp, $sStr, $matchesValue);
                $value = isset($matchesValue[1]) ? $matchesValue[1] : '';
            }
            $name = preg_replace('/^"([^"]*)"$|^\'([^\']*)\'$/', '$1$2', $nameInput) ;
        } //value = /[^\w\-]checked[^\w\-]/i.test(str) ? getParam(str, nullVal, nullVal, valueRegExp, valueReplace) || 'on' : undefined;

        else if ($selInput) {
            $name = preg_replace('/^"([^"]*)"$|^\'([^\']*)\'$/', '$1$2', $selInput) ;

        }
        if ($value != 'undefined') {
            $aResult[$name] = $value;
        }
    }
    if (preg_match('/type=\"submit\" value=\"(.*?)\"/i', $htmlForm, $matchesSubmit)) {
        //   print_r($matchesSubmit);
    }

    return $aResult;
}
function endsWith($str, $suffix) {
    if(strrpos($str, $suffix, -1)  === false){
        return false;
    }
    return true;
}


function joinUrl($url, $path) {
    if (!$path) //Пустой путь
        return $url;
    if (preg_match('/^\//', $path)){
        //Абсолютный путь
        return preg_replace('/^(\\w+:\\/\\/[\\w.\\-]+).*$/', '$1'.$path, $url );
    }

    if (preg_match('/^\w+:\/\//', $path)) {
        return $path;
    }
    //относительный путь
    $url = preg_replace('/\\?.*$/', '', $url);

    if (preg_match('/:\\/\\/.*\\//', $url)){
        $url =    $url = preg_replace('/\\/[^\\/]*$/', '/', $url);
    }
    if (!endsWith($url, '/'))
        $url .= '/';
    return $url + $path;
}

function redirectIfNeeded($html, $url){
    //Потребовался редирект формой...
    if(preg_match('/<body[^>]+onload[^>]+submit/i', $html, $matches)){
        $aPost = ParseForm($html);
        $action = '';
        if(preg_match('/<form[^>]+action=[\'"]([^\'"]*)/', $html, $matchesAction)){
            $action = $matchesAction[1];
        }
        $url = joinUrl($url, $action);
        $aHeadersNew = array_merge($GLOBALS['aHeaders'], array('Refefer: '.$url));

        $html  = CURL($url, $aPost, $aHeadersNew);
        $html = mb_convert_encoding($html, 'UTF-8');

        print_r($html);

    }

    //return $html;
}

/*********************/



$url = "https://lk.ssl.mts.ru/";
curl_setopt($ch, CURLOPT_URL, $url);
$html = curl_exec($ch);


$html = redirectIfNeeded($html, $url);
//åprint_r($html);

return;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stimulate, 2018-07-19
@Stimulate

first remove Accept-Encoding: gzip, deflate, sdch, br to get uncompressed text

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question