-1

I'm having trouble understanding how CURL handles headers.

I have a site.com/page1 that I want to access with CURL it does a 308 redirect to site.com/page2/file.zip

What I need is to go through site.com/page1 with CURL but download site.com/page2/file.zip directly from site.com

I'm using this code but it does not work as expected. It accesses site.com/page1 redirects to site.com/page2/file.zip but opens the file in the browser

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $_cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $_cookie_file);
curl_setopt($ch, CURLOPT_REFERER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, true);

curl_exec($ch);
$error = curl_getinfo($ch);
curl_close($ch);

I guess if I can keep the response headers I'll fix the problem. But how do I do it ?? How do I use the same headers for the CURL visitor that the site I am accessing is sending me.

Bruno Andrade
  • 565
  • 1
  • 3
  • 17

2 Answers2

0

You want the cURL option RETURNTRANSFER set to true so what is returned comes back to you. Since you are trying to save a ZIP file you'll also need to open a file and use the CURLOPT_FILE option to tell cURL where to save your ZIP file.

curl_setopt ($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt ($ch, CURLOPT_TIMEOUT,'180');  # 3 minute timeout
$FileOut = fopen('MyZIP_File.zip','w') or die('Could not open the output data file');
curl_setopt ($ch, CURLOPT_FILE,$FileOut);
curl_exec   ($ch);
fclose($FileOut) or die('We ran into a problem saving data file');
Dave
  • 5,108
  • 16
  • 30
  • 40
  • I do not want to save the Zip file on my server I want it to open the URL to save it on the computer direct from the URL. – Bruno Andrade Mar 29 '19 at 10:39
  • 1
    @BrunoAndrade please explain what you mean by “the computer” then. Simply the machine that you are running this PHP/cURL code on? Then you still need to save the received data to a file, that doesn’t happen _automagically_ … cURL got you the data, you still need to properly instruct it what to _do_ with it next. – 04FS Mar 29 '19 at 10:47
  • Machine where I'm running CURL is the server. Computer is the visitor's machine using CURL. For simplicity I just want to know how to keep my headers. If I get a 308 code I want CURL to pass this code 308 to the visitor. – Bruno Andrade Mar 29 '19 at 10:53
  • This is making less sense by the minute. Why can't the visitor simply go to the first page themselves and be redirected? Why is cURL even in the picture? – Dave Mar 29 '19 at 11:04
0

This solved the problem.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $_cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $_cookie_file);
curl_setopt($ch, CURLOPT_REFERER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, true);

$result = curl_exec($ch);
if (preg_match('~Location: (.*)~i', $result, $match)) {
   $location = trim($match[1]);
   header('Location:' . $location);
}
Bruno Andrade
  • 565
  • 1
  • 3
  • 17