PHP/CURL как получить страницы после авторизации
Всем здравствуйте
Стала задача залогиниться с помощью CURL и CSRF. Вроде все проходит хорошо, результат возвращается true. Но потом я пытаюсь загрузить другую страницу сайта и мне выдает форму логина, то есть то ли авторизироваться не получилось, то ли я где-то что-то теряю в процессе И как вообще понять, что авторизация прошла успешно?
$username = "user";
$password = "pass";
$url = "https://url.com/";
$cookie= "c:\\cookies.txt";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_errno($ch)) die(curl_error($ch));
$dom = new DomDocument();
$dom->loadHTML($response);
$tokens = $dom->getElementsByTagName("meta");
for ($i = 0; $i < $tokens->length; $i++)
{
$meta = $tokens->item($i);
if($meta->getAttribute('name') == 'csrf-token')
$token = $meta->getAttribute('content');
}
$postinfo = "email=".$username."&password=".$password."&_csrf=".$token;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo);
$html = curl_exec($ch);
// Получаю новую страницу
$newUrl = "https://url.com/new_my_url";
curl_setopt($ch, CURLOPT_URL, $newUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
$html1 = curl_exec($ch);
if (curl_errno($ch))
{ echo "ERROR! <br><hr><br>";
print curl_error($ch);
}
curl_close($ch);
В файле cookies.txt хранится такое
# Netscape HTTP Cookie File # https://curl.haxx.se/docs/http-cookies.html # This file was generated by libcurl! Edit at your own risk.
#HttpOnly_.url.com TRUE / FALSE 0 _urlcom_session "тут куча данных"