Curl Cookie Headers

Discussion in 'PHP' started by Im The ONE, Feb 24, 2008.

  1. #1
    Greetings,
    I'm trying to download file to my server using curl but I am unable to get it working like I want possibly for the reason that cookie header is not sent correctly.

    here's my code:
      
        $head .= "Cookie: user=xxxxxxxx-xxxxxxxx";
        $ch = curl_init();
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/ Firefox 2.0.0.11, ProductSub: 20071127, RV version: 1.8.1.11');          
        curl_setopt($ch, CURLOPT_FILE, $out);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $head);
        curl_setopt($ch, CURLOPT_URL, $file);
    
    PHP:
    its downloading html page while it should be downloading the file
     
    Im The ONE, Feb 24, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Try:
    
    curl_setopt($ch, CURLOPT_COOKIE, 'user=xxxxxxxx-xxxxxxxx');
    
    PHP:
    EDIT:

    Your version could possibly work too. There's a small error:
    
    curl_setopt($curl, CURLOPT_HTTPHEADER, $head);
    
    PHP:
    It should be $ch, not $curl.
     
    nico_swd, Feb 24, 2008 IP
  3. Im The ONE

    Im The ONE Peon

    Messages:
    800
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Great, its working thanks ;)
    but I face another problem now its redirecting after accepting the cookie variables to other link and the file ends up as 300byte redirection headers
     
    Im The ONE, Feb 24, 2008 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    Add this:
    
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    
    PHP:
     
    nico_swd, Feb 24, 2008 IP
    Im The ONE likes this.
  5. Im The ONE

    Im The ONE Peon

    Messages:
    800
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Still gives 302 Moved header file
     
    Im The ONE, Feb 25, 2008 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Hm, can you post your full code? And can you post the exact output you get?
     
    nico_swd, Feb 25, 2008 IP
  7. Im The ONE

    Im The ONE Peon

    Messages:
    800
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    0
    #7
    the code:
    
    <?php   get_rapidshare("http://rapidshare.com/files/86205767/Pro_PHP_XML_and_Web_Services.rar", "/", "xyz3.rar");
    
    function get_rapidshare($file, $path, $filename)
    {
        echo "<br>Attempting message download for $file<br>";
        $out = fopen($filename, 'wb');
    
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //like you told
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/ Firefox 2.0.0.11, ProductSub: 20071127, RV version: 1.8.1.11');          
        curl_setopt($ch, CURLOPT_FILE, $out);
        curl_setopt($ch, CURLOPT_COOKIE, 'user=xxxx-xxxx');
        curl_setopt($ch, CURLOPT_URL, $file);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt ($ch, CURLOPT_HEADER, 1); 
        curl_exec($ch);
        curl_close($ch);
    
    }
    ?>
    PHP:
    here's what I get
    
    HTTP/1.1 302 Moved Temporarily
    P3P: CP="ALL DSP COR CURa ADMa DEVa TAIa PSAa PSDa IVAa IVDa CONa TELa OUR STP UNI NAV STA PRE"
    Date: Mon, 25 Feb 2008 07:59:07 GMT
    Connection: close
    Accept-Ranges: bytes
    Location: http://rs181l32.rapidshare.com/files/54852047/Pro_PHP_XML_and_Web_Services.rar
    Content-Length: 0
    
    PHP:
     
    Im The ONE, Feb 25, 2008 IP