找回密码
 立即注册
搜索
楼主: admin

福建厦门

[复制链接]

0

主题

126

回帖

310

积分

等离子电视迷

积分
310
发表于 2025-7-22 10:39:34 | 显示全部楼层
谢谢,非常感谢

0

主题

57

回帖

196

积分

彩电迷

积分
196
发表于 2025-7-22 11:09:57 | 显示全部楼层
代理了切片!

0

主题

57

回帖

196

积分

彩电迷

积分
196
发表于 2025-7-22 11:19:53 | 显示全部楼层
版权原因不直播

0

主题

10

回帖

32

积分

黑白电视迷

积分
32
发表于 2025-7-22 12:07:18 | 显示全部楼层
福建厦门

0

主题

79

回帖

206

积分

等离子电视迷

积分
206
发表于 2025-7-22 12:27:01 | 显示全部楼层

0

主题

70

回帖

203

积分

等离子电视迷

积分
203
发表于 2025-7-24 13:20:35 | 显示全部楼层
不错,看看效果

0

主题

63

回帖

172

积分

彩电迷

积分
172
发表于 2025-7-25 01:19:11 | 显示全部楼层
福建厦门

0

主题

31

回帖

66

积分

彩电迷

积分
66
发表于 2025-7-25 09:51:21 | 显示全部楼层
群主什么专业出生 厉害了

0

主题

31

回帖

66

积分

彩电迷

积分
66
发表于 2025-7-25 09:58:17 | 显示全部楼层
<?php
if (need_m3u8($id, $ts_url)) {
    $u = get_m3u8_url($id);
    $c = send_request($u, $ct);
    $c = replace_ts_urls($u, $c);
} else {
    $c = send_request($ts_url, $ct);
}
echo_content($ct, $c);



function need_m3u8(&$id, &$ts_url)
{
    $q = $_SERVER['QUERY_STRING'];
    $r = stripos($q, 'id=') === 0;
    if ($r) {
        $id = $_GET['id'];
        // 将频道名称转换为数字ID
        $id = convert_channel_name_to_id($id);
    } else {
        $ts_url = $q;
    }
    return $r;
}

// 频道名称到ID的映射函数
function convert_channel_name_to_id($channel_name) {
    // 频道映射表
    $channel_map = [
        'xmws' => '84',    // 厦门卫视
        'xmtv1' => '16',   // 厦视一套
        'xmtv2' => '17',   // 厦视二套
        'xmtv4' => '52'    // 厦门电视台移动电视
    ];

    // 如果是频道名称,转换为ID;如果是数字ID,直接返回
    if (isset($channel_map[$channel_name])) {
        return $channel_map[$channel_name];
    } else {
        // 如果不在映射表中,检查是否为数字ID
        if (is_numeric($channel_name)) {
            return $channel_name;
        } else {
            // 未知频道名称,返回默认值或抛出错误
            return '84'; // 默认返回厦门卫视
        }
    }
}

function get_m3u8_url($id)
{
    $r = load_from_cache($id);
    if ($r)
        return $r;

    $r = get_m3u8_url_from_web($id);

    save_to_cache($id, $r);
    return $r;
}

function get_m3u8_url_from_web($id) {
    $u = "https://mapi1.kxm.xmtv.cn/api/v1/channel.php?channel_id=".$id;
    $c = send_request($u);
    $j = json_decode($c);
    return $j[0] -> channel_stream[0] -> m3u8;
}

function save_to_cache($id, $url)
{
    $a[$id]['expire_time'] = time() + 60 * 60;
    $a[$id]['url'] = $url;
    array_to_file($a, 'xmtv_cache.txt');
}

function load_from_cache($id)
{
    file_to_array('xmtv_cache.txt', $a);
    if (isset($a) && isset($a[$id]) && $a[$id]['expire_time'] > time())
        return $a[$id]['url'];
    else
        return '';
}

function file_to_array($filename, &$array) {
    $array = [];
    if (file_exists($filename)) {
        $handle = fopen($filename, 'r');
        if (flock($handle, LOCK_SH)) { // 共享锁,允许其他进程读但禁止写
            $data = file_get_contents($filename);
            $array = unserialize($data);
            flock($handle, LOCK_UN);
        }
        fclose($handle);
    }
    return true;
}

function array_to_file($array, $filename) {
    $data = serialize($array);
    file_put_contents($filename, $data, LOCK_EX);
    return true;
}

function send_request($url, &$content_type = null)
{
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_REFERER, 'https://seexm2024.kxm.xmtv.cn/');
    $res = curl_exec($ch);
    if (func_num_args() > 1)
        $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
    curl_close($ch);
    return $res;
}

function replace_ts_urls($m3u8_url, $m3u8_content)
{
    $dest_ts_path = dirname($m3u8_url)."/";
    $dest_ts_domain_path = implode("/", array_slice(explode("/", $m3u8_url), 0, 3));
    $protocol = !empty($_SERVER['HTTPS']) ? 'https' : 'http';
    $self_part = "$protocol://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
    return preg_replace_callback("/^((?!#).+)$/im",
        function ($matches) use ($self_part, $dest_ts_path, $dest_ts_domain_path) {
            if (!is_absolute_url($matches[1]))
                if ($matches[1][0] == '/')
                    $ts = $dest_ts_domain_path.$matches[1];
                else
                    $ts = $dest_ts_path.$matches[1];
            else
                $ts = $matches[1];
            return "$self_part?$ts";
        },
        $m3u8_content
    );
}

function is_absolute_url($url) {
    return stripos($url, 'http:') === 0 || stripos($url, 'https:') === 0;
}

function echo_content($content_type, $content)
{
    header("Access-Control-Allow-Origin: *");
    header("Content-Type: $content_type");
    echo $content;
}

XX.php?id=xmws xmtv1 xmtv2 xmtv4

点评

要的就是频道和代码分离啊。你把频道加到代码里,代码又长了不少!  发表于 2025-7-29 12:17

评分

参与人数 1海贝 +1 收起 理由
admin + 1 赞一个!

查看全部评分

0

主题

35

回帖

81

积分

彩电迷

积分
81
发表于 5 天前 | 显示全部楼层
看看怎么样
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋

创办于 2025 年 5 月 5 日

快速回复 返回顶部 返回列表