user3824329 February 2016

Download a ZIP file using cURL

I am trying to download a ZIP file using cURL, from a given URL. I received an URL from a supplier where I should download a ZIP file. But everytime I try to download the ZIP file I get the page that says that I am not logged in.

The url where I should get the file from looks like this:

https://www.tyre24.com/nl/nl/user/login/userid/USERID/password/PASSWORD/page/L2V4cG9ydC9kb3dubG9hZC90L01nPT0vYy9NVFE9Lw==

Here you see that the USERID, and PASSWORD are variables that are filled in with the correct data. The strange thing is that if I enter the URL in my browser it seems to work, the zip file is getting downloaded.

But everytime I call that URL with cURL, I seem to get a incorrect login page. Could someone tell me what I am doing wrong?

It seems like that there is a redirect behind the given URL, that is why I have putted in the cURL call: curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

Here is my code:

set_time_limit(0);

//File to save the contents to
$fp = fopen ('result.zip', 'w+');

$url = "https://www.tyre24.com/nl/nl/user/login/userid/118151/password/5431tyre24/page/L2V4cG9ydC9kb3dubG9hZC90L01nPT0vYy9NVFE9Lw==";

//Here is the file we are downloading, replace spaces with %20
$ch = curl_init(str_replace(" ","%20",$url));

curl_setopt($ch, CURLOPT_TIMEOUT, 50);

//give curl the file pointer so that it can write to it
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

$data = curl_exec($ch);//get curl response

//done
curl_close($ch);

Am I doing something wrong?

Answers


yahoo February 2016

Include following lines of code after curl_init() .i think this will work.

CURLOPT_RETURNTRANSFER ::: TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.

CURLOPT_USERAGENT::The contents of the "User-Agent: " header to be used in a HTTP request.

Read more about curl_setopt here.

 $ch = curl_init(str_replace(" ","%20",$url));
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); 


RomanPerekhrest February 2016

To download a zip file from the external source via CURL use one of the following approaches:

First approach:

function downloadZipFile($url, $filepath){
     $ch = curl_init($url);
     curl_setopt($ch, CURLOPT_HEADER, 1);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
     $raw_file_data = curl_exec($ch);

     if(curl_errno($ch)){
        echo 'error:' . curl_error($ch);
     }
     curl_close($ch);

     file_put_contents($filepath, $raw_file_data);
     return (filesize($filepath) > 0)? true : false;
 }

downloadZipFile("http://www.colorado.edu/conflict/peace/download/peace_essay.ZIP", "result.zip");

A few comments:

  • to get data back from the remote source you have to set CURLOPT_RETURNTRANSFER option
  • instead of consequent calls of fopen ... fwite functions you can use file_put_contents which is more handy

And here is screenshot with result.zip which was downloaded a few minutes earlier using the above approach:

result

Second approach:

function downloadZipFile($url, $filepath){
     $fp = fopen($filepath, 'w+');
     $ch = curl_init($url);

     curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
     curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
     //curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
     curl_setopt($ch, CURLOPT_FILE, $fp);
     curl_exec($ch);

     curl_close($ch);
     fclose($fp);

     return (filesize($filepath) > 0)? true : false;
 }

Post Status

Asked in February 2016
Viewed 3,652 times
Voted 6
Answered 2 times

Search




Leave an answer