problem with cookie in Curl

Asked

Viewed 124 times

1

I am developing a login with Curl and I am making two requests, the first one makes the post to the action page of the form and generates a txt file with the cookie, soon after I make a request using this file to access the protected page. but I do not have access to the page, the system makes a redirect to the error page.

  $tmpfname = dirname(__FILE__).'/cookies.txt';

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_HEADER, 1);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
  curl_setopt($ch, CURLOPT_TIMEOUT, 120);
  curl_setopt($ch, CURLOPT_POST,true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($ch, CURLOPT_VERBOSE, 1);
  curl_setopt($ch, CURLOPT_STDERR, $f);
  curl_setopt($ch, CURLOPT_COOKIEJAR, $tmpfname );
  curl_setopt($ch, CURLOPT_COOKIEFILE, $tmpfname );
  $redirectURL = curl_getinfo($ch,CURLINFO_EFFECTIVE_URL );
  $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  curl_setopt($ch, CURLOPT_ENCODING, "");
  $err = curl_error($ch);
  $ret = curl_exec($ch);
  curl_close ($ch);


  $chl = curl_init();
  curl_setopt($chl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($chl, CURLOPT_COOKIEFILE, $tmpfname );
  curl_setopt($chl, CURLOPT_URL, $urlll);
  curl_setopt($chl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  curl_setopt($chl, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($chl, CURLOPT_HEADER, 1);
  curl_setopt($chl, CURLOPT_VERBOSE, 1);
  curl_setopt($chl, CURLOPT_CONNECTTIMEOUT, 120);
  curl_setopt($chl, CURLOPT_TIMEOUT, 120);
  curl_setopt($chl, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($chl, CURLOPT_ENCODING, "");
  $er = curl_error($chl);
  $hy = curl_exec($chl);
  curl_close ($chl);
  var_dump($hy);
  echo $hy;
  • My first observation is: Do you really need all this? , you monitored the site you are trying to log in with cURL to see the requests to be made? I say this because I can login via cURL on twitter and instagram without having to use all this curl_setopt.

  • Good morning William. I tried to do with a call but as the system makes several redirects it does not reach the last page (protected page).

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.