Download a file with cURL
<p>I am trying to download a file with cURL. I have received an URL where the file is located, but the url makes an redirect before reaching the file. For some reason I always receive the logout page when I access the URL with cURL, but when I enter the URL directly in my browser the file just downloads as it is supposed to. The file that should be downloaded is a RAR file, but instead of the file I get the incorrect login page.</p>

<p>This the current code:</p>

<pre><code>$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "url");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true); 
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
$result = curl_exec($ch); 

curl_close($ch);

print_r($result);
</code></pre>

<p>As you can see I am using the following code to allow the redirects: </p>

<pre><code>curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
</code></pre>

<p>But I always receive the incorrect login page from the website if I use the above code. Can anyone see what I am doing wrong here ?</p>

<p>This is the reponse I get from the server:</p>

<pre><code>HTTP/1.1 302 Found
Date: Tue, 26 Jan 2016 15:33:18 GMT
Server: Apache/2.2.3 (Red Hat)
X-Powered-By: PHP/5.3.2
Set-Cookie: PHPSESSID=session_id; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Location: /nl/nl/export/download/t/Mg==/c/MTQ=/
Vary: Accept-Encoding
Content-Length: 0
Content-Type: text/html; charset=UTF-8
</code></pre>