啊码一般废话比较少,直接甩代码,不懂扣我,再不懂就只能扣脚丫了,OK?
/**
* 将远程资源保存到本地
* @param string $source 资源地址
* @param string $local 本地地址
* @return array
*/
private function execDown($source,$local)
{
if (!is_dir(dirname($local))) {
mkdir(dirname($local),0755,true);
}
$localFile = fopen($local, 'w');
$ch = curl_init(); // 初始化 cURL 会话
curl_setopt($ch,CURLOPT_URL,$source); // 远程地址
curl_setopt($ch, CURLOPT_POST, 0); // 非POST请求
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3000); // 最长等待连接成功时间
curl_setopt($ch, CURLOPT_FILE, $localFile); // 本地路径
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // https请求 不验证证书
curl_exec($ch);
if (curl_error($ch)) {
$res = [
'status' => 0,
'message' => curl_error($ch),
'time' => 0,
'fileinfo' => []
];
fclose($localFile);
curl_close($ch);
return $res;
} else {
$info = curl_getinfo($ch);
curl_close($ch);
fclose($localFile);
if (filesize($local) != $info['size_download']) {
$res = [
'status' => 0,
'message' => '下载数据不完整,请重新下载',
'time' => $info['total_time'],
'fileinfo' => []
];
return $res;
}
$res = [
'status' => 1,
'message' => '下载完成',
'time' => $info['total_time'],
'fileinfo' => [
'file_type' => $info['content_type'],
'file_size' => $info['size_download'],
'remote_path' => $source,
'local_path' => $local,
]
];
return $res;
}
}