<?php
set_time_limit(3600);
$postFieldsLogin = require ('config.php');
$assembleStr = function($array){
    $str = '';
    foreach($array as $key => $value) {
        $str.= $key . '=' . $value . '&';
    }
    return $str = rtrim($str, '&');
};
$domain = 'https://w...content-available-to-author-only...e.ru';
$linkLogin = '/Account/Login';
$linkGetCsv = '/GetItems/Content?=&';
$todayMinusDays = 1;
$searchSubStr = 'Здравствуйте';
$cookie = 'cookie.txt';
$csv = 'content.csv';
$log = 'log.log';
$data = $fieldsString = '';
$sep = "\n";
$timesOfRepeatSep = 2;
$sleepTime = 180;
$postFieldsGetCsv = [
    'account' => '',
    'beginDate' => date('d.m.Y',
        (strtotime('-'. $todayMinusDays .' day', strtotime(date('r'))))) ,
    'endDate' => date('d.m.Y')
];
//truncate files
file_put_contents($log, '');
file_put_contents($cookie, '');
file_put_contents($csv, '');

do {
    /*initial login request*/
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $domain . $linkLogin);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_COOKIESESSION, true);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    $fieldsString = $assembleStr($postFieldsLogin);
    curl_setopt($ch, CURLOPT_POST, count($postFieldsLogin));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fieldsString);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    $data.= str_repeat($sep, $timesOfRepeatSep) . trim(curl_exec($ch));
    $data.= str_repeat($sep, $timesOfRepeatSep) . trim($fieldsString);
    file_put_contents($log, trim($data), FILE_APPEND);
    if (mb_strpos($data, $searchSubStr)) {
        $isLogin = true;
        /*another request for getting formatted content*/
        curl_setopt($ch, CURLOPT_URL, $domain . $linkGetCsv);
        $fieldsString = $assembleStr($postFieldsGetCsv);
        curl_setopt($ch, CURLOPT_POST, count($postFieldsGetCsv));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fieldsString);
        $result = curl_exec($ch);
        curl_close($ch);
        $data.= str_repeat($sep, $timesOfRepeatSep) . trim($result);
        $data.= str_repeat($sep, $timesOfRepeatSep) . trim($fieldsString);
        file_put_contents($csv, $result);
        file_put_contents($log, trim($data), FILE_APPEND);
    }
    else {
        $data.= str_repeat($sep, $timesOfRepeatSep) .
            trim($sleepTime . " SECS DELAY") . str_repeat($sep, $timesOfRepeatSep);
        file_put_contents($log, trim($data), FILE_APPEND);
        sleep($sleepTime);
    }
}
while (!$isLogin);