Answer the question
In order to leave comments, you need to log in
PHP script is not authorized, what parameters to pass?
I am writing a small parser that will go through a ready-made list of links to products and take current prices from there, but the problem is that the catalog is closed and available only to authorized users. Here is a piece of code with authorization functions:
<?php
// функция авторизации и сохранения куков
function post_content ($url,$postdata) {
$uagent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)";
$ch = curl_init( $url );
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLOPT_USERAGENT, $uagent); // useragent
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postdata));
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__)."/cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__)."/cookie.txt");
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
}
// функция обращения к страницам после авторизации, используя сохраненные куки
function get_web_page( $url )
{
$uagent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)";
$ch = curl_init( $url );
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // возвращает веб-страницу
curl_setopt($ch, CURLOPT_HEADER, 0); // не возвращает заголовки
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // переходит по редиректам
curl_setopt($ch, CURLOPT_ENCODING, ""); // обрабатывает все кодировки
curl_setopt($ch, CURLOPT_USERAGENT, $uagent); // useragent
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120); // таймаут соединения
curl_setopt($ch, CURLOPT_TIMEOUT, 120); // таймаут ответа
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); // останавливаться после 10-ого редиректа
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__)."/cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__)."/cookie.txt");
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
}
// так выглядит тело запроса:
// username=tronmarina&password=tronmarina&remember=yes&Submit=%D0%92%D0%BE%D0%B9%D1%82%D0%B8&option=com_users&task=user.login&return=aW5kZXgucGhwP0l0ZW1pZD00MzU%3D&23b0f12487d33d530f63635857f2dd1e=1: undefined
$url = 'http://zelart.com.ua/';
$result = post_content( $url, array( // вызываем функцию авторизации с параметрами
'username'=>'tronmarina',
'password'=> 'tronmarina',
'remember' => 'yes',
'Submit' => '%D0%92%D0%BE%D0%B9%D1%82%D0%B8',
'option' => 'com_users',
'task' => 'user.login'));
$html = $result['content'];
echo $html;
?>
Answer the question
In order to leave comments, you need to log in
In order to carry out authorization, you must do the following:
1) Download the page.
1.1) You need to get COOKIEs that will return with the page
1.2) Get ALL fields from the form (2 more additional parameters are passed there that you don’t have)
2) Send a request with COOKIE, parameters from p.1.2 to the authorization url (to the same download page)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question