亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 140 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號