@senilo

Проблема с авторизацией при помощи CURL PHP?

Господа, добрый день. Имеется сайт rating.rucoop.ru/secondary
Требуется авторизация при помощи CURL
Есть следующий код:
<?php 
		mb_internal_encoding('UTF-8');
  function auth($url, $cookie, $post)
  {
  	$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url); 
		curl_setopt($curl, CURLOPT_HEADER, true);
	  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
	  curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
	  curl_setopt($curl, CURLOPT_TIMEOUT, 30); 
	  curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'); 
	  curl_setopt($curl, CURLOPT_POST, true);
	  curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
	  curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie); 
	  curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie);
	  return curl_exec($curl);
  }

  function read_brs($url, $last_url, $cookie)
  {
  	$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url); 
		curl_setopt($curl, CURLOPT_REFERER, $last_url);
		curl_setopt($curl, CURLOPT_HEADER, true);
	  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
	  curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
	  curl_setopt($curl, CURLOPT_TIMEOUT, 30); 
	  curl_setopt($curl, CURLOPT_POST, 0);
	  curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.0; ru; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3'); 
	  curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie);
	  curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie);
	  return curl_exec($curl);
	 }

 	$cookie_file = realpath("cookie.txt");
	$post = "utf8=✓&user[email]=test&user[password]=12345&commit=Войти";
	$url = 'http://rating.rucoop.ru/secondary/users/sign_in';
	$newUrl = "http://rating.rucoop.ru/secondary/ratings/for_group";

  echo auth($url, $cookie_file, $post);
	echo read_brs($newUrl, $url, $cookie_file);
?>


Мы вроде бы как сохраняем куку, но все при переходи на нужную страницу сайта, перекидывает на страницу авторизацией.
Если как-то написал не так, не кидайте камнями, я только учусь) Всем добра)
  • Вопрос задан
  • 324 просмотра
Пригласить эксперта
Ответы на вопрос 1
SerJook
@SerJook
кодер
Там стоит защита с помощью CSRF-токена, вам нужно получить его и отправлять в заголовках запроса.
Нужно повторно использовать сессию CURL.

Набросал вам код:

Код
<?php 
    mb_internal_encoding('UTF-8');
        
    function get_token($curl, $url, $cookie) {
        curl_setopt($curl, CURLOPT_URL, $url); 
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);    
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); 
        curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie); 
        curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie);
        $response = curl_exec($curl);
    
        if (preg_match('/<meta content="([^"]+)" name="csrf-token"/s', $response, $matches)) {
           return $matches[1];
        } 
    }

  function auth($curl, $url, $cookie, $post, $csrf_token)
  {
      $headers = array(
        'X-CSRF-Token: '.$csrf_token,
        'X-Requested-With: XMLHttpRequest'
      );
    curl_setopt($curl, CURLOPT_URL, $url); 
    curl_setopt($curl, CURLOPT_HEADER, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 30); 
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'); 
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie); 
    curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    return curl_exec($curl);
  }

  function read_brs( $curl, $url, $last_url, $cookie)
  {
    curl_setopt($curl, CURLOPT_URL, $url); 
    curl_setopt($curl, CURLOPT_REFERER, $last_url);
    curl_setopt($curl, CURLOPT_HEADER, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 30); 
    curl_setopt($curl, CURLOPT_POST, 0);
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.0; ru; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3'); 
    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie);
    curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie);
    return curl_exec($curl);
   }

    $curl = curl_init();
  $cookie_file = realpath("cookie.txt");
  $post = "utf8=✓&user[email]=Pogodinaa&user[password]=12345&commit=Войти";
  $url = 'http://rating.rucoop.ru/secondary/users/sign_in';
  $newUrl = "http://rating.rucoop.ru/secondary/ratings/for_group";
  $csrf_token = get_token($curl, 'http://rating.rucoop.ru/secondary/',$cookie_file);
  echo auth($curl, $url, $cookie_file, $post, $csrf_token);
  echo read_brs($curl, $newUrl, $url, $cookie_file);
?>
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы