php - Download a ZIP file using cURL -
i trying download zip file using curl, given url. received url supplier should download zip file. everytime try download zip file page says not logged in.
the url should file looks this:
https://www.tyre24.com/nl/nl/user/login/userid/userid/password/password/page/l2v4cg9ydc9kb3dubg9hzc90l01npt0vyy9nvfe9lw==
here see userid, , password variables filled in correct data. strange thing if enter url in browser seems work, zip file getting downloaded.
but everytime call url curl, seem incorrect login page. tell me doing wrong?
it seems there redirect behind given url, why have putted in curl call: curl_setopt($ch, curlopt_followlocation, true);
here code:
set_time_limit(0); //file save contents $fp = fopen ('result.zip', 'w+'); $url = "https://www.tyre24.com/nl/nl/user/login/userid/118151/password/5431tyre24/page/l2v4cg9ydc9kb3dubg9hzc90l01npt0vyy9nvfe9lw=="; //here file downloading, replace spaces %20 $ch = curl_init(str_replace(" ","%20",$url)); curl_setopt($ch, curlopt_timeout, 50); //give curl file pointer can write curl_setopt($ch, curlopt_file, $fp); curl_setopt($ch, curlopt_followlocation, true); $data = curl_exec($ch);//get curl response //done curl_close($ch);
am doing wrong?
to download zip file external source via curl
use 1 of 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 data remote source have set
curlopt_returntransfer
option - instead of consequent calls of
fopen
...fwite
functions can usefile_put_contents
more handy
and here screenshot result.zip
downloaded few minutes earlier using above approach:
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; }
Comments
Post a Comment