上海教育电视台PHP源码

原创 1,107 次阅读 278 字 预计阅读时间: 1 分钟 发布于 2023-12-14 最后更新于 2024-12-18


前言

经测试,上海教育电视台官网源为静态直播源,m3u8和ts均为referer验证;官方EPG接口支持4*24小时的回放调用,回放链接无限制。
本文源码采用常规的m3u8代理、ts转发方法。由于官方自带EPG,回放调用的时间戳处理采用的是近似匹配方式,未做特殊处理。

源码

<?php
//Written by Wheiss
error_reporting(0);
date_default_timezone_set("PRC");
$ts = $_GET['ts']??'';
if ($ts){
	$decodedUts = urldecode($ts);
	$data = get_curl($decodedUts);
	header('Content-Type: video/MP2T');
	header("Content-Disposition: inline; filename={$ts}.ts");
} else {
	$playseek = $_GET['playseek']??'';
	if ($playseek) {
		$t_arr = explode('-',$playseek);
		$starttime = strtotime($t_arr[0]);
		$endtime = strtotime($t_arr[1]);
		$url1 = "https://vod.setv.sh.cn/api?action=listLives&cid=82B382BDD579F54480C6C886D781C21C&uid=912CD5647E383F55A976490CF817CB6C&details=1&page_size=200&sort[]=no+asc&sort[]=created_time+desc&type[]=video&referer=https:%2F%2Fwww.setv.sh.cn";
		$d = json_decode(file_get_contents($url1))->result[0]->record_epg;
		$d1 = json_decode($d,true);
		$ct = count($d1);
		for ($i=0;$i<$ct;++$i) {
			if (abs($starttime-$d1[$i]['time'])<121) {
				$j = $i+1;
				if ($j>$ct) {
					break;
				} elseif (abs($endtime-$d1[$j]['time'])<121) {
					break;
				}
			}
		}
		$path = $d1[$i]['path'];
		if (!$path) die('未匹配到回放链接!');
		$url = 'https://vod.setv.sh.cn'.$path.'.m3u8?type=vod';
		header("location: $url");
		exit;
	} else {
		$https = isset($_SERVER['HTTPS'])?'https':'http';//当前请求的主机使用的协议。
		$http_host = $_SERVER['HTTP_HOST'];//当前请求的主机名。
		$requestUri = $_SERVER['REQUEST_URI'];//获取当前请求的 URI
		$decodedUri = urldecode($requestUri);//URL解码
		$Uripath = explode('?',$decodedUri)[0];//strstr($decodedUri,'?',true);
		$urlp = "{$https}://{$http_host}{$Uripath}?ts=";
		$url = 'https://live2018.setv.sh.cn/setv/programme10_ud.m3u8?auth_key=4794048378-0-0-06e60dd476e18047abd3be7701993ee8';
		$m3u8 = get_curl($url);
		$urlpath = dirname($url).'/';
		$m3u8s = explode("\n",trim($m3u8));
		$data = '';
		foreach($m3u8s as $m3u8l){
			if (stripos($m3u8l,'.ts')!==false){
				$data .= $urlp.urlencode($urlpath.$m3u8l).PHP_EOL;
			} else {
				$data .= $m3u8l.PHP_EOL;
			}
		}
		header("Content-Type: application/vnd.apple.mpegurl");
		header("Content-Disposition: inline; filename=index.m3u8");
	}
}
echo $data;
exit;

function get_curl($url){
	$ch = curl_init($url);  
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
	curl_setopt($ch, CURLOPT_REFERER, 'https://www.setv.sh.cn/');
	$data = curl_exec($ch);
	curl_close($ch);
	return $data;
}
?>

补充说明

URL格式

  • 直播示例:http://127.0.0.1/setv.php
  • 回放示例:http://127.0.0.1/setv.php?playseek=20241217190000-20241217193000