PHP CURL Download File

后端 未结 3 2041
逝去的感伤
逝去的感伤 2020-12-06 10:10

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          


        
相关标签:
3条回答
  • 2020-12-06 10:49

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

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
    
    0 讨论(0)
  • 2020-12-06 10:55
    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);
    0 讨论(0)
  • 2020-12-06 10:58

    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);
    
    0 讨论(0)
提交回复
热议问题