Я пытаюсь войти на сайт с помощью PHP/curl, чтобы иметь доступ к данным, доступным только зарегистрированным пользователям на сайте. Форма запрашивает адрес электронной почты, пароль и скрытое значение безопасности. Значение безопасности генерируется случайным образом при каждой загрузке страницы входа. Я думаю, что это вызывает мою проблему: в приведенном ниже скрипте curl сначала запрашивает страницу, код анализирует значение безопасности, а затем ломает попытки входа в систему. Однако, как я проверял с помощью print_r, код безопасности будет изменен для вторая загрузка страницы.Cookies и скрытые значения безопасности в curl
Вот код:
$email = urlencode("emailhandleIuse");
$password = "myPassword";
$loginURL = "https://www.example.com/login/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $loginURL);
$ch = setCurlParameters($ch);
$securityCode = curl_exec($ch);
$securityCode = urlencode(scrape_between($securityCode, "<input type=\"hidden\" name=\"security\" value=\"","\" />")); // gets security value from the page HTML
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'email='.$email.'&password='.$password.'&security='.$securityCode);
curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, "https://www.example.com/followingpage");
$secondPage = curl_exec($ch);
curl_close($ch);
function setCurlParameters($ch) {
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
return $ch;
}
function scrape_between($data, $start, $end){
$data = stristr($data, $start); // Stripping all data from before $start
$data = substr($data, strlen($start)); // Stripping $start
$stop = stripos($data, $end); // Getting the position of the $end of the data to scrape
$data = substr($data, 0, $stop); // Stripping all data from after and including the $end of the data to scrape
return $data; // Returning the scraped data from the function
}
$ SecondPage завершаться быть Войти страницу, потому что я не проверку после размещения деталей я думаю, необходимо.
Любая помощь очень ценится.