17

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

Dean Rather
  • 31,756
  • 15
  • 66
  • 72
user3376311
  • 479
  • 1
  • 4
  • 9

4 Answers4

30

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);
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
user3376311
  • 479
  • 1
  • 4
  • 9
  • 1
    This didn't work for me, ended up going with this solution which worked: http://stackoverflow.com/questions/31107851/how-to-fix-curl-35-cannot-communicate-securely-with-peer-no-common-encryptio – Robert Sinclair Oct 15 '16 at 18:51
  • I am getting empty data. But I am able to download the file directly from the browser – Kiren S Apr 03 '19 at 13:31
  • @user3345547 it was the issue with permission of the file. Once given the permission I was able to download it. – Kiren S Aug 05 '20 at 03:47
2

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

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85
1
  1. Did you check if result actually contains data?
  2. with fputs on larger files you need to specify byte length
  3. Try using file_put_contents($destination, $data);
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
Rok Nemet
  • 136
  • 7
  • 1. did echo on $data and its empty, I don't know why. do you have any idea why when uses the browser I get a download dialog but with this it doesn't work? – user3376311 Mar 03 '14 at 20:06
  • 1
    Seeing you are requesting through SSL. If you don't need to specify the login for that URL then set the CURL option CURLOPT_SSL_VERIFYPEER to false. Also check what the error returned is with $error = curl_error($ch); then print it out – Rok Nemet Mar 03 '14 at 20:21
  • got this error: Unknown SSL protocol error in connection to myapps.gia.edu:443 – user3376311 Mar 03 '14 at 20:30
  • Set CURLOPT_SSL_VERIFYPEER to false curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); – Rok Nemet Mar 03 '14 at 20:31
0

See if you can use the following code to help you.

//Create a cURL handle. $ch = curl_init($fileUrl);

//Pass our file handle to cURL. curl_setopt($ch, CURLOPT_FILE, $fp);

TBR920
  • 77
  • 7