上传
if (class_exists('\CURLFile')) {
$field = array('fieldname' => new \CURLFile(realpath($filepath)));
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
} else {
$field = array('fieldname' => '@' . realpath($filepath));
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
}
下载
$ch = \curl_init($url);
\curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
\curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
\curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArr);
\curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
\curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
\curl_setopt($ch, CURLOPT_TIMEOUT, 3600);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 20971520);
$flag=0;
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch ,$str) use (&$flag){
$len = strlen($str);
$flag++;
$type = \curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
if($flag==1){
$size = \curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
$type = \curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
$httpcode = \curl_getinfo($ch, CURLINFO_HTTP_CODE);
\header("HTTP/1.1 ".$httpcode);
\header("Content-Type: ".$type);
\header("Content-Length: ".$size);
\header('Content-Transfer-Encoding: binary');
\header('Cache-Control:max-age=2592000');
}
echo $str;
return $len;
});
$output = \curl_exec($ch);
https://blog.csdn.net/dengjiexian123/article/details/53057593