Copy Image from Remote Server Over HTTPS

2019-07-20 14:02发布

问题:

I already find answers how to copy images over HTTP, but when I try to copy images over HTTPS then I get this:

Warning: copy(): SSL operation failed with code 1. OpenSSL Error messages: error:14077458:SSL routines:SSL23_GET_SERVER_HELLO:reason(1112)

This is code I use:

copy('https://www.metalacmarket.com/product-img/org/JpUSP3KgvgeeikNheRDi4CRg.jpg', IMAGES_PATH.'JpUSP3KgvgeeikNheRDi4CRg.jpg');

Any idea how to get images over HTTPS?

回答1:

You could use cURL.

Here's an example adapted from the basic curl example.

$source = 'https://www.metalacmarket.com/product-img/org/JpUSP3KgvgeeikNheRDi4CRg.jpg';
$target = 'image.jpg';

$ch = curl_init($source);
$fp = fopen($target, "wb");

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);
curl_close($ch);
fclose($fp);