Спасибо что поделились, всё утро промудохался с авторизацией.
Обернул логин в еще одну функцию, для удобства нескольких авторизаций.
login('https://www.fl.ru', 'логин', 'пароль');
function login($site, $login, $password) {
$result = getURL($site);
$cookie = $result['set_cookie'];
$post = array(
'autologin' => '1',
'login' => $login,
'passwd' => $password,
'u_token_key' => preg_replace('/(^.+TOKEN_KEY = \')(\w+)(\'.+$)/s', '$2', $result['content'])
);
$result = getURL($site . '/login/', $cookie, $post);
$cookie = $result['set_cookie'];
echo $result['content'];
}
function getURL($url, $cookie = false, $post = false, $timeout = 30, $maxRedirs = 3)
{
$ch = curl_init();
$header[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3";
$header[] = "Pragma: ";
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
if ($cookie) {
if (is_array($cookie)) {
foreach($cookie as $key => $value ) {
$cookieString[] = "{$key}={$value}";
};
$cookie = implode('; ', $cookieString);
}
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
}
if ($post){
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
$content = curl_exec($ch);
$response = curl_getinfo($ch);
curl_close ($ch);
if ($content) {
preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $content, $match);
$setCookie = array();
if ($match[1]) {
foreach($match[1] as $setCookieString) {
parse_str($setCookieString, $setCookieUnit);
$setCookie = array_merge($setCookie, $setCookieUnit);
}
$cookie = is_array($cookie) ? array_merge($cookie, $setCookie) : $setCookie;
}
}
if (($response['http_code'] == 301 || $response['http_code'] == 302) AND $maxRedirs)
if ($response['redirect_url'])
return getURL($response['redirect_url'], $cookie, $post, $timeout, --$maxRedirs);
if ($content) {
return array(
'set_cookie' => $cookie,
'response' => $response,
'content' => $content
);
} else
return false;
}