为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
肥皂起泡泡 2021-11-13 16:41:12
前段时间我在网上发现了这段代码,可以通过我的服务器流式传输远程视频文件,除了在 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');}
查看完整描述

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;


?>


查看完整回答
反对 回复 2021-11-13
  • 1 回答
  • 0 关注
  • 129 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信