Redirect with curl

0

I'm trying to login to a curl account but it does not go to the final page.

Site map:

  • form page.
  • page that receives the post (the curl is stopped here). 200 OK
  • Restricted page (curl does not arrive here). 302 Object moved
  • Does anyone know why curl does not access or go through this page?

    My code.

    $f = fopen('cookies.txt', 'w');
    $fp = fopen("cookies.txt", "w");
    fclose($fp);
    
    
     $url = "";
    
     $postfields = array();
     $postfields[''] = '';
     $postfields[''] = ''; 
     $postfields[''] = '';
     $postfields[''] = ''; 
     $postfields[''] = '';
     $postfields[''] = ''; 
     $postfields[''] = '';
     $postfields[''] = '';
    
     $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_FOLLOWLOCATION, true);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     //curl_setopt($ch, CURLOPT_COOKIESESSION, true);
     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_POSTFIELDS, json_encode(array()));
     //curl_setopt($ch, CURLOPT_USERAGENT, $useragent); 
     curl_setopt($ch, CURLOPT_POSTREDIR, -1);
     curl_setopt($ch, CURLOPT_VERBOSE, 1);
     curl_setopt($ch, CURLOPT_MAXREDIRS, -1);
     //curl_setopt($ch, CURLOPT_AUTOREFERER, true);
     curl_setopt($ch, CURLOPT_STDERR, $f);
     curl_setopt($ch, CURLOPT_COOKIEJAR, $tmpfname );
     curl_setopt($ch, CURLOPT_COOKIEFILE, $tmpfname );
     //curl_setopt($ch, CURLOPT_REFERER, $url);
     $redirectURL = curl_getinfo($ch,CURLINFO_EFFECTIVE_URL );
     $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
     $status_url = curl_getinfo($ch , CURLINFO_EFFECTIVE_URL);
     curl_setopt($ch, CURLOPT_ENCODING, "");
     $err = curl_error($ch);
     //ob_start();
     $ret = curl_exec($ch); // Get result after login page.
     $redirectedUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
     curl_close ($ch);
    
      echo $ret;
    
        
    asked by anonymous 07.10.2018 / 05:04

    1 answer

    1

    You need to set the CURL parameter, where it allows you to follow the redirects:

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    

    As you wanted to limit the redirects you can use this parameter:

    curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
    
        
    07.10.2018 / 15:25