1

I'm trying to write a login function using PHP CURL to perform a login in the following site: https://partners.mrfavorit.com/. When I look at the network requests, it seems that the platform is on .NET.

First I make one GET request to collect cookies:

$ch = curl_init();
curl_setopt_array( $ch, array(
  CURLOPT_URL => 'https://partners.mrfavorit.com/',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HEADER => true,
));
$a = curl_exec($ch);
preg_match_all('|Set-Cookie: (.*);|U', $a, $results);
$cookies2 = implode(';', $results[1]);

then I send POST with the login credentials to the same URL, but it doesn't work, request returns 200 with the login page html code.

curl_setopt_array($ch, array(
  CURLOPT_URL => "https://partners.mrfavorit.com/?&txtUserName=myusername&txtPassword=mypassword123",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_HEADER => true,
  CURLOPT_POSTFIELDS =>'',
  CURLOPT_HTTPHEADER => array(
    "Content-Type: application/json",
    "Cookie: ".$cookies2
  ),
));

$r2 = curl_exec($ch);
print_r($r2);

What could be the reason for this to be failing?

The final purpose for this is to collect data from the report tables inside the system.

  • 1
    Have a look at https://stackoverflow.com/questions/12885538/php-curl-and-cookies – Nigel Ren May 07 '20 at 12:23
  • without looking, you might need to be posting the fields not passing them as GET params – Lawrence Cherone May 07 '20 at 12:23
  • also should be using cookiejar instead of implementing it in headers – Lawrence Cherone May 07 '20 at 12:24
  • I have tried with `CURLOPT_COOKIEJAR` and the functions from the other post, but nothing works. I think the ASP.NET does not allow this to be done. –  May 08 '20 at 11:57
  • 1
    https://stackoverflow.com/questions/14551194/how-are-parameters-sent-in-an-http-post-request You are supposed to set username and password in the body. As they use a http form. – Alex May 23 '20 at 14:40
  • @nikolay Do you know exactly all the values that are supposed to be sent to the CURLOPT_URL? What are they and what kind of request does the CURLOPT_URL accept, POST or GET? I inspected the provided page and noticed there are hidden form fields meaning there is the possibility that you are not sending all needed values to the URL. Get us all the needed values according to the API provisioned. – OmniPotens May 26 '20 at 06:10

2 Answers2

3

I made it work. The trick was to make initial GET request to the login page, and get the cookies, as well as the "__VIEWSTATE", "__VIEWSTATEGENERATOR", "__EVENTVALIDATION" dynamic fields. Then POST to the same URL, with these fields.

After that I had to make Two POST requests to /dailyreport.aspx while getting the view state and the rest variables from the first, and passing them to the second. Only then I was able to retrieve the HTML table that was returned.

2

you should put your POST data inside CURLOPT_POSTFIELDS =>'' and CURLOPT_URL should point to the login URL. The way you do it is requesting the site with GET request.

$post = ['txtUserName'=>'myUserName','txtPassword'=>'myPassword123'];    
curl_setopt_array($ch, array(
      CURLOPT_URL => "https://partners.mrfavorit.com/",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "POST",
      CURLOPT_HEADER => true,
      CURLOPT_POSTFIELDS =>http_build_query($post),
      CURLOPT_HTTPHEADER => array(
        "Content-Type: application/json",
        "Cookie: ".$cookies2
      ),
    ));

    $r2 = curl_exec($ch);
    print_r($r2);

see this PHP + curl, HTTP POST sample code?

[EDIT] adding http_build_query() to $post will generate a application/x-www-form-urlencoded HTTP request

see the comment on Posting an array with curl_setopt

zimorok
  • 234
  • 1
  • 9
  • i have tested this, no success still. Did you try it? –  May 25 '20 at 10:42
  • or you can use `http_build_query()` with the `$post`, so it will `CURL_POSTFIELDS => http_build_query($post)`, besides the username/password, I think there are other data that you need to POST along with the user/password combination – zimorok May 26 '20 at 03:27