PHP 远程流式传输不适用于 iOS(Safari、Chrome)

前段时间我在网上发现了这段代码,可以通过我的服务器流式传输远程视频文件,除了在 iOS(iPhone 6S、iOS 12.4)上,它运行得很好。访问了大量建议使用“Accept-Ranges”、“Content-Length”和“Content-Type”标头的线程,这些标头已在代码中实现。iOS 拒绝在 Safari 和 Chrome 上流式传输文件。


任何帮助将不胜感激!


编辑#1


显然我的服务器没有正确返回范围。Apple 通常首先要求提供部分内容,例如“Range: bytes=0-1”。目前它一直在等待代码的响应。还验证了它不适用于 macOS 的 Safari。哦苹果。


ini_set('max_execution_time', 0);

$useragent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36";

$v = 'https://notmywebsite/remotevideo.mp4';


$ch = curl_init();

curl_setopt($ch, CURLOPT_VERBOSE, 1);

curl_setopt($ch, CURLOPT_TIMEOUT, 222222);

curl_setopt($ch, CURLOPT_URL, $v);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

curl_setopt($ch, CURLOPT_HEADER, true);

curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

curl_setopt($ch, CURLOPT_USERAGENT, $useragent);

curl_setopt($ch, CURLOPT_NOBODY, true);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$info = curl_exec($ch);

$size2 = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);


$filesize = $size2;

$offset = 0;

$length = $filesize;


header("Content-Type: video/mp4");


if (isset($_SERVER['HTTP_RANGE'])) {

    $partialContent = "true";

    preg_match('/bytes=(\d+)-(\d+)?/', $_SERVER['HTTP_RANGE'], $matches);

    $offset = intval($matches[1]);

    $length = $size2 - $offset - 1;

} else {

    $partialContent = "false";

}

if ($partialContent == "true") {

    header('HTTP/1.1 206 Partial Content');

    header('Accept-Ranges: bytes');

    header('Content-Range: bytes '. $offset .

        '-' . ($offset + $length) . 

        '/'. $filesize);

} else {

    header('Accept-Ranges: bytes');

}



肥皂起泡泡
浏览 130回答 1
1回答

蓝山帝景

我确实找到了解决方案。上面的代码很乱,所以我清理了一下。它现在适用于 macOS 和 iOS!<?phpheader('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 responsecurl_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'])) {&nbsp; &nbsp; //If ranges are requested, perform a reg match and extract them&nbsp; &nbsp; preg_match('/bytes=(\d+)-(\d+)?/', $_SERVER['HTTP_RANGE'], $matches);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $fr = intval($matches[1]);&nbsp; &nbsp; $sr = is_null($matches[2]) ? $end : intval($matches[2]);&nbsp; &nbsp; $headers = array("Range: $matches[0]");&nbsp; &nbsp; header('HTTP/1.1 206 Partial Content');&nbsp; &nbsp; header("Content-Range: bytes {$fr}-{$sr}/{$filesize}");&nbsp; &nbsp; //Here we set the headers (ranges) for the remote video request&nbsp; &nbsp; 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;?>
打开App,查看更多内容
随时随地看视频慕课网APP