gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 15:49:25 26 4
gpt4 key购买 nike

我前段时间在网上发现这段代码可以通过我的服务器流式传输远程视频文件,除了在 iOS(iPhone 6S、iOS 12.4)上,它工作得很好。访问了大量建议使用“Accept-Ranges”、“Content-Length”和“Content-Type” header 的线程,这些 header 已在代码中实现。 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');
}

header("Content-length: ". $size2);

if (isset($_SERVER['HTTP_RANGE'])) {
// if the HTTP_RANGE header is set we're dealing with partial content
$partialContent = true;
// find the requested range
// this might be too simplistic, apparently the client can request
// multiple ranges, which can become pretty complex, so ignore it for now
preg_match('/bytes=(\d+)-(\d+)?/', $_SERVER['HTTP_RANGE'], $matches);
$offset = intval($matches[1]);
$length = $filesize - $offset - 1;
$headers = array(
'Range: bytes=' . $offset .
'-' . ($offset + $length) .
''
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}

curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 222222);
curl_setopt($ch, CURLOPT_URL, $v);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, false);
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, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_exec($ch);

最佳答案

我确实找到了解决方案。上面的代码很乱所以我整理了一下。它现在适用于 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;

?>

关于PHP 远程流式传输不适用于 iOS(Safari、Chrome),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57353120/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com