|
本帖最后由 jiteloozz 于 2022-7-4 00:05 编辑
首先上代码:
- <?php
- header('Content-Type: text/json;charset=UTF-8');
- $id = isset($_GET['id'])?$_GET['id']:'ggpd';
- $mn= array(
- 'xwzh' => 16,//大同新闻综合
- 'ggpd' => 8,//大同公共
- 'mdsh' => 9,//大同煤都生活
- 'jtgb' => 14,//交通可视化广播
- 'xwzhgb' => 13,//新闻综合可视化广播
- 'jjwygb' => 15,//经济文艺可视化广播
- );
- switch(date('i')%3){
- case 0:
- $data=get_data('http://mapi.dtradio.com.cn/api/v1/program.php?channel_id='.$mn[$id]);
- preg_match('/(.*?)playlist.m3u8?(.*?)"/',$data,$str);
- $live=stripslashes(substr($str[1],strrpos($str[1],"http")));
- $playurl = $live.'playlist.m3u8'.$str[2];
- break;
- case 1:
- $data=get_data('http://mapi.dtradio.com.cn/api/v1/channel.php?category_id=0&channel_id='.$mn[$id]);
- $playurl=json_decode($data)[0]->m3u8;
- break;
- default:
- $data=get_data('http://mapi.dtradio.com.cn/api/v1/channel.php?category_id=1%2C3');
- foreach(json_decode($data) as $sv){
- if($sv->id==$mn[$id]){
- $playurl= $sv->m3u8;
- }
- }
- break;
- }
- if(!$live){
- $live=strstr($playurl,'playlist.m3u8',true);
- }
- $refurl=preg_replace('/(.*?m3u8)/',"$live$1",get_data($playurl));
- $playurl=strstr($refurl,'http');
- //print_r(get_data($playurl));//获取不到TS数据
- header('Location:'.$playurl);
- function get_data($url){
- $header=array(
- "User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:50.0) Gecko/20100101 Firefox/50.0",
- "Referer: https://www.dtradio.com.cn/local/folder25",
- );
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- $data = curl_exec($ch);
- curl_close($ch);
- return $data;
- }
复制代码
向大神请教求解问题一:
pc端POT、手机端浏览器都能正常运行PHP并播放电视和广播;但是奇怪的是PC端黑鸟播放器、Blank播放器都只能播放广播,不能播放电视。。。Why?
向大神请教求解问题二:
尝试获取TS来解决问题一,但突然悲催的发现,尝试了多种方法,curl和file_get_contents()都获取不到ts数据,是不是代码还缺少特定请求?
print_r(get_data($playurl)); //问题:curl不到ts数据
貌似第一次遇上这种情况,特向大神请教求解,Thanks~~~
后记:问题二已解决
37句获取不到数据,是因为36句有个坑。
36句,37句,38句替换成
$playurl=trim(strstr($refurl,'http'));
$host='https://'.parse_url($playurl)[host];
print_r(preg_replace('/(..\/..\/)/',"$host/",get_data($playurl)));
问题二一解决,问题一就不是事了!!!
修正后代码,回复可见:
测试地址:http://mmitv.top/other/dttv.php?id=jtksh
- <?php
- header('Content-Type: text/json;charset=UTF-8');
- $id = isset($_GET['id'])?$_GET['id']:'jtksh';
- $mn= array(
- 'xwzh' => 16, //大同新闻综合
- 'ggpd' => 8, //大同公共
- 'mdsh' => 9, //大同煤都生活
- 'jtksh' => 14, //交通可视化广播
- 'xwksh' => 13, //新闻综合可视化广播,大多数处于未播黑屏状态
- 'jjksh' => 15, //经济文艺可视化广播,大多数处于未播黑屏状态
- 'jtgb' => 12, //大同交通广播
- 'xwgb' => 10, //大同新闻广播
- 'jjgb' => 11, //大同经济广播
- );
- switch(intval(date('s')/20)){
- case 0://接口一
- $data=get_data('http://mapi.dtradio.com.cn/api/v1/program.php?channel_id='.$mn[$id]);
- preg_match('/(.*?)playlist.m3u8?(.*?)"/',$data,$str);
- $live=stripslashes(substr($str[1],strrpos($str[1],"http")));
- $playurl = $live.'playlist.m3u8'.$str[2];
- break;
- case 1://接口二,推荐
- $data=get_data('http://mapi.dtradio.com.cn/api/v1/channel.php?category_id=0&channel_id='.$mn[$id]);
- $playurl=json_decode($data)[0]->m3u8;
- break;
- default://接口三
- $data=get_data('http://mapi.dtradio.com.cn/api/v1/channel.php?category_id=1%2C2%2C3');
- foreach(json_decode($data) as $sv){
- if($sv->id==$mn[$id]){
- $playurl= $sv->m3u8;
- }
- }
- break;
- }
- if($mn[$id]>12&&$mn[$id]<16){
- header('Location:'.$playurl);
- exit;
- }else{
- //用截取方法会踩坑,会忽略字符串中的\n等字符; //用preg_match('/\w*(.*?)$/')方式获取302地址不会踩坑。
- preg_match('/\w*(.*?)$/', get_data($playurl), $site);
- $playurl=preg_split('/playlist.m3u8/',$playurl)[0].$site[0];
- $host=parse_url($playurl)['host'];
- print_r(preg_replace('/(..\/..\/)/',"http://$host/",get_data($playurl)));
- exit;
- }
- function get_data($url){
- $header=array(
- "User-Agent: Mozilla/5.0",
- "Referer: https://www.dtradio.com.cn/local/folder25",
- );
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- $data = curl_exec($ch);
- curl_close($ch);
- return $data;
- }
复制代码
|
|