Download image from SSL using curl?

How to download image from curl (https site)?

The file is saved on my computer, but why is it empty (0KB)?

function save_image($img,$fullpath){
    $ch = curl_init ($img);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0); 
    $rawdata=curl_exec($ch);
    curl_close ($ch);

    $fp = fopen($fullpath,'w');
    fwrite($fp, $rawdata);
    fclose($fp);
}

save_image("https://domain.com/file.jpg","image.jpg");
+4
source share
2 answers
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);

should be:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

therefore, curl knows that it should return data, not output it back.

Also, sometimes you need to do one more job to get the curl accepted SSL certificates:
Using cURL in PHP to access secure HTTPS sites (SSL / TLS)

EDIT:

Given your use, you should also set CURLOPT_HEADER to false, as Alix Axel recommended.

SSL , , , , SSL, Alix, , , , , CURL .

+5

:

curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

, CURLOPT_HEADER false CURLOPT_RETURNTRANSFER true.

+3

All Articles