N
N
Nikita Kornilov2019-12-25 13:37:28
PHP
Nikita Kornilov, 2019-12-25 13:37:28

How to log in using CURL on a site with ASP.NET?

Good afternoon! I already asked two questions on the same case, received an answer to them and thought that I could handle it myself, but it wasn’t there, I still run into some dead ends. Please help again)
In general, you need to parse data from the auto parts site . There is a search for several articles at once, but it is available only after authorization. I'm trying to write a PHP script that will log in to this site and, in fact, look for the necessary spare parts there and parse them.
The usual functionality of Simple HTML DOM for this, of course, is not enough. I realized that I need to use cURL, and wrote the following code:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://parts.major-auto.ru/SearchNew/ByList');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIE, 'ASP.NET_SessionId=abcdefghij12345677890...; .ASPXAUTH=DSFHJSDFDSG6785DFG78DF6G78DF6G7896D8F9G7D...');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
  'searchElements' => $data,
  'SearchByList' => 'Поиск',
]);

$html = curl_exec($ch);
curl_close($ch);

While I substitute the session data in curl_setopt($ch, CURLOPT_COOKIE, '...') manually, I authorize on the site through the browser, track the request in the "Network" tab and take this data from there.
5e033a15c676b138674945.png
How can this process be automated? And then to do so every time the session ends is not the case. The authorization form sends a request to the page: /Account/LogOn . I wrote the following script to send data to it with a login and password:
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://parts.major-auto.ru/Account/LogOn');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).'/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE,  dirname(__FILE__).'/cookie.txt');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
  'UserName' => '+7(123)456-78-90',
  'Password' => 'password',
  'btnLogOn' => 'Вход',
]);

$html = curl_exec($ch);

echo 'HTTP код ответа сервера: ' . curl_getinfo($ch, CURLINFO_HTTP_CODE) . '<br>';

curl_close($ch);

But it doesn’t work - I get an error in response from the server. How can the problem be solved? I saw that the words ASP.NET and .ASPXAUTH appear in the cookies, maybe this will help somehow?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shamanov, 2019-12-25
@SilenceOfWinter

In Firefox/Chrome, you can get requests in the curl format: web development > network > right-click on the request and select copy > copy as curl. well, based on these data, we form a curl request in php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question