F
F
FootWork2014-08-04 09:18:04
PHP
FootWork, 2014-08-04 09:18:04

How to make a php parser using cURL with authorization?

Good afternoon, the second day I suffer with the parser, I can’t understand why it doesn’t work.

<?php

$url = 'http://fantasts.ru/forum/index.php';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url); // отправляем на 

curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (бла бла бла..) "); 

curl_setopt($ch, CURLOPT_HEADER, 0); // пустые заголовки

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // следовать за редиректами

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);// таймаут4

curl_setopt($ch, CURLOPT_REFERER, "http://fantasts.ru/forum/index.php");

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// просто отключаем проверку сертификата 

curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).'/my_cookies.txt'); // сохранять куки в файл

curl_setopt($ch, CURLOPT_COOKIEFILE,  dirname(__FILE__).'/my_cookies.txt');

curl_setopt($ch, CURLOPT_POST, 1); // использовать данные в post

$login = array(

  'UserName' => 'Тут ваш логин',

  'PassWord' => 'Ваш пароль',

  'x' => 0,

  'y' => 0

curl_setopt(($ch), CURLOPT_POSTFIELDS, $login);

curl_exec($ch); 

if (curl_errno($ch)) 

{

   print curl_error($ch);

   exit;

} 

curl_close($ch);

?>

As you can see, I am not sending headers. In this case, the parser gives me the page from which the login was made. The cookie is written to the file. If I add browser title immetation lines, i.e.:
$header  = array
(
  'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  'Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3',
  'Accept-Encoding: gzip, deflate',
  'Content-type: application/x-www-form-urlencoded'
);

curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

he gives me all sorts of krakozyabry.
Help to disassemble on the example of the above site, so that everything is clear.
There are suspicions that he writes the wrong cookies. The site sends a cookie to the main page to determine the last login to the site.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
F
FootWork, 2014-08-08
@FootWork

Finished. As a result, the authorization code turned out like this:

<?php

$url = 'http://fantasts.ru/forum/index.php?act=Login&CODE=01&CookieDate=1';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); // отправляем на 
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0"); 
curl_setopt($ch, CURLOPT_HEADER, 0); // пустые заголовки
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // возвратить то что вернул сервер
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // следовать за редиректами
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);// таймаут4
curl_setopt($ch, CURLOPT_REFERER, "http://fantasts.ru/forum/index.php");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// просто отключаем проверку сертификата 
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).'/my_cookies.txt'); // сохранять куки в файл
curl_setopt($ch, CURLOPT_COOKIEFILE, '/my_cookies.txt');
curl_setopt($ch, CURLOPT_POST, 1); // использовать данные в post
$login = array(
  'UserName' => 'YourLogin',
  'PassWord' => 'YourPass'
);
curl_setopt(($ch), CURLOPT_POSTFIELDS, $login);
$result1 = curl_exec($ch);
$url2 ='http://fantasts.ru/forum/index.php?';
curl_setopt($ch, CURLOPT_URL, $url2);
curl_setopt($ch, CURLOPT_REFERER, "http://fantasts.ru/forum/index.php?act=Login&CODE=01&CookieDate=1");
curl_setopt($ch, CURLOPT_COOKIEFILE, '/my_cookies.txt');
$result2 = curl_exec($ch);
if (curl_errno($ch)) 
{
   print curl_error($ch);
   exit;
}
else
{
  echo 'LOGIN WAS SUCCESFULL';
  echo $result2;
}
curl_close($ch);

?>

Explain only why when I put
Everything is fine (although how does it work without headers??)
And if I try to be even more like a browser, i.e. writing
$header  = array
(
    'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3',
    'Accept-Encoding: gzip, deflate',
    'Content-type: application/x-www-form-urlencoded'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

At the output I get krakozyabry that no decoder understands

D
Dmitry Skogorev, 2014-08-04
@EnterSandman

curl_setopt($ch, CURLOPT_HEADER, 0); // empty headers
^ wow wow. you at least read what this option does.
when you don't know why it doesn't work - take Wireshark. You make a request with a browser, you make a request with your script. See what is the difference between requests and add your script to simulate the work of the browser.
As Dmitry already said - you need to contact fantasts.ru/forum/index.php?s=SID&act=Login&CODE=0...
plus, to get goodies, it is recommended to first just pick up the page, then do POST

D
Dmitry Entelis, 2014-08-04
@DmitriyEntelis

Let's start with the fact that the authorization form leads to

http://fantasts.ru/forum/index.php?act=Login&amp;CODE=01&amp;CookieDate=1
not http://fantasts.ru/forum/index.phplike in your code.
What are the fields x, y - they are not in the form.

V
vkeyster, 2014-08-04
@vkeyster

<?php
$url = 'http://fantasts.ru/forum/index.php?act=Login&CODE=01&CookieDate=1'; //логин по этой ссылке
$ch = curl_init();
$header  = array
(
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding: gzip, deflate', // указиваем серверу, что ответ надо сжимать
'Content-type: application/x-www-form-urlencoded'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL, $url); // отправляем на
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (бла бла бла..) ");
curl_setopt($ch, CURLOPT_HEADER, 0); // не включать заголовк в результат запроса
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // следовать за редиректами
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);// таймаут4
curl_setopt($ch, CURLOPT_REFERER, "http://fantasts.ru/forum/index.php");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// просто отключаем проверку сертификата
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).'/my_cookies.txt'); // сохранять куки в файл
curl_setopt($ch, CURLOPT_COOKIEFILE,  dirname(__FILE__).'/my_cookies.txt');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate'); // указивает curl что ответ может быть сжат, без этого будут "кракозябры"
curl_setopt($ch, CURLOPT_POST, 1); // использовать данные в post
$login = array(
'UserName' => 'login',
'PassWord' => 'password',
'x' => 0,
'y' => 0);
curl_setopt(($ch), CURLOPT_POSTFIELDS, http_build_query($login)); // для вашей формы надо так
curl_exec($ch);
if (curl_errno($ch))
{
print curl_error($ch);
exit;
}
curl_close($ch);
?>

A
Alexander Borisovich, 2014-08-04
@Alexufo

Damn, why are people so lazy to read about the instrument they use? When you buy a phone, do you run to the seller for every question? Of course, it will throw you out of the authorization, do not transfer the cookies received after authorization to further requests. There are just a LOT of examples on the net.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question