Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP CURL Download File

im trying to download a file from a url, when I use the browser the download dialog works but when I use this code the new file on my server stay empty.

$ch = curl_init();
$source = "https://myapps.gia.edu/ReportCheckPortal/downloadReport.do?reportNo=$row['certNo']&weight=$row['carat']";
curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec ($ch);
curl_close ($ch);
$destination = "./files/certs/$row['certNo'].pdf";
$file = fopen($destination, "w+");
fputs($file, $data);
fclose($file);

example of url: https://myapps.gia.edu/ReportCheckPortal/downloadReport.do?reportNo=1152872617&weight=1.35

like image 914
user3376311 Avatar asked Mar 03 '14 19:03

user3376311


People also ask

Can I use curl in PHP?

cURL is a PHP library and command-line tool (similar to wget) that allows you to send and receive files over HTTP and FTP. You can use proxies, pass data over SSL connections, set cookies, and even get files that are protected by a login.

How do I download files with curl?

How to download a file with curl command. The basic syntax: Grab file with curl run: $ curl https://your-domain/file.pdf. Get file using ftp or sftp protocol: $ curl ftp://ftp-your-domain-name/file.tar.gz.


2 Answers

I solved this problem using this:

curl_setopt($ch, CURLOPT_SSLVERSION,3);

This is the final code:

$source = "https://myapps.gia.edu/ReportCheckPortal/downloadReport.do?reportNo=1152872617&weight=1.35";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSLVERSION,3);
$data = curl_exec ($ch);
$error = curl_error($ch); 
curl_close ($ch);

$destination = "./files/test.pdf";
$file = fopen($destination, "w+");
fputs($file, $data);
fclose($file);
like image 62
user3376311 Avatar answered Oct 15 '22 12:10

user3376311


Your url using https connection. Use the following curl option as well.

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
like image 28
Sabuj Hassan Avatar answered Oct 15 '22 13:10

Sabuj Hassan