//保存文件方法
function saveAsImage($url, $file, $path)
{
$filename = pathinfo($url, PATHINFO_BASENAME);
$resource = fopen($path . $filename, 'a');
fwrite($resource, $file);
fclose($resource);
}
//下载文件方法
function downloadImage($url, $path)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
//避免https 的ssl验证
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSLVERSION, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$file = curl_exec($ch);
if(curl_exec($ch) === false) echo 'Curl error: ' . curl_error($ch);
curl_close($ch);
saveAsImage($url, $file, $path);
}
//调用方法
$goods_image="https://www.baidu.com/img/baidu_resultlogo@2.png";
//下载文件到本地服务器
downloadImage($goods_image, 'images/');