|
<?php// 频道ID映射关系$n = [ 'lyzh' => '111841', //临沂综合 'lyjj' => '115062', //临沂经济 'lygg' => '113571' //临沂公共];$headers = [ 'Referer: https://www.ilinyi.net/'];
$currentFile = basename($_SERVER['SCRIPT_FILENAME']);// 获取请求参数$id = isset($_GET['id']) ? $_GET['id'] : 'lyzh';$tsParam = isset($_GET['ts']) ? $_GET['ts'] : null;// 根据ID获取对应的频道号,如果找不到则使用默认值$channelId = isset($n[$id]) ? $n[$id] : '111841';$url = "https://m3u8-channel.lytv.tv/nmip-media/channellive/channel{$channelId}/playlist.m3u8";if ($tsParam) { // 如果传入了 ts 参数,则转发对应的 ts 文件 header('Content-Type: video/mp2t'); // 初始化 curl 来获取 ts 文件 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $tsParam); curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_exec($ch); curl_close($ch);} else { // 如果没有传入 ts 参数,则获取 m3u8 内容 header('Content-Type: application/vnd.apple.mpegurl'); header('Access-Control-Allow-Origin: *'); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $response = curl_exec($ch); curl_close($ch); // 处理 m3u8 内容,将 ts 链接替换为本地转发链接 $lines = explode("\n", $response); $output = []; foreach ($lines as $line) { if (strpos($line, '.ts') !== false && strpos($line, 'http') === 0) { // 如果是 ts 链接,则替换为本地转发链接,并附带 id 参数 $output[] = "{$currentFile}?id={$id}&ts=" . urlencode($line); } elseif (strpos($line, '.ts') !== false && strpos($line, 'http') !== 0 && strpos($line, '/') !== 0) { // 相对路径的 ts 链接 $baseUrl = dirname($url); $fullTsUrl = $baseUrl . '/' . $line; $output[] = "{$currentFile}?id={$id}&ts=" . urlencode($fullTsUrl); } else { $output[] = $line; } } echo implode("\n", $output);} |
|