1 回答
TA贡献1843条经验 获得超7个赞
我确实找到了解决方案。上面的代码很乱,所以我清理了一下。它现在适用于 macOS 和 iOS!
<?php
header('Content-Type: video/mp4');
header('Accept-Ranges: bytes');
$agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36';
$url = 'https://notmywebsite/remotevideo.mp4';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_REFERER, $url); //Remote video might need it to get proper response
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
$info = curl_exec($ch);
$filesize = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
$end = $filesize - 1;
if (isset($_SERVER['HTTP_RANGE'])) {
//If ranges are requested, perform a reg match and extract them
preg_match('/bytes=(\d+)-(\d+)?/', $_SERVER['HTTP_RANGE'], $matches);
$fr = intval($matches[1]);
$sr = is_null($matches[2]) ? $end : intval($matches[2]);
$headers = array("Range: $matches[0]");
header('HTTP/1.1 206 Partial Content');
header("Content-Range: bytes {$fr}-{$sr}/{$filesize}");
//Here we set the headers (ranges) for the remote video request
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_exec($ch);
curl_close($ch);
exit;
?>
- 1 回答
- 0 关注
- 129 浏览
添加回答
举报