简单的php下载电影脚本,http状态码为206
下载站range不能自定义,只能传递初始值,根据下载站自行调试代码,如果能自定义range就更简单了,少了很多判断
private function curl($url, $header)
{
$fh = fopen(storage_path("mp4/{$fileName}.mp4"), 'a');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
// 视频数据写入文件
curl_setopt($ch, CURLOPT_FILE, $fh);
$output = curl_exec($ch);
$curlGetInfo = curl_getinfo($ch);
// 文件总大小
$length = data_get($curlGetInfo, 'download_content_length');
// http状态码
$responseCode = data_get($curlGetInfo, 'http_code');
// 获取剩余多少
$errorInfo = curl_error($ch);
curl_close($ch);
fclose($fh);
preg_match('/\d+/', $errorInfo, $withNum);
$withNum = data_get($withNum, 0);
return [
'code' => $responseCode,
'length' => $length,
'with' => $withNum
];
}
$rang = 0;
$length = 0;
// 循环获取
while (true) {
$header = [];
$data = $this->curl($url, $header);
if (data_get($data, 'code') != 206) {
echo '获取失败', PHP_EOL;
break;
}
if ($rang == 0) {
$length = data_get($data, 'length');
}
// 没有剩余 则下载完成
$with = data_get($data, 'with');
if ($with <= 0) {
echo '获取完成', PHP_EOL;
break;
}
$rang = $length - $with;
}