<?php
namespace Home\Controller;
use Think\Controller;
// use Org\Util\Image;
class PolyvController extends Controller {
private $weburl = "http://ztn.sychjy.cn";
private $appId = "gia6b2aghv";
private $appsecret = "40694b81e7fa123123a3628bdc065478";
private $userid = "a824433880";
//直播接入配置
public function config(){
$class_child_id = I('class_child_id');
$channel_id = M('class_child')->where(['id'=>$class_child_id])->getField('channel_id');
$data = [
'appId'=>$this->appId,//appid
'channelId'=> $channel_id,
'timestamp' =>$this->tretime(),//13位时间戳
];
$server = $this->appsecret;
// 签名
$sign = $this->sort($data,$server);
$result = [
'appId'=>$this->appId,
'timestamp' =>$this->tretime(),//13位时间戳
'userId'=>$this->userid,
'sign' =>$sign,
'channelId'=>$channel_id,//获取频道号
];
$this->ajaxReturn(['code'=>200,'data'=>$result]);
}
// //创建频道
// public function create_pd(){
// //请求头
// $data = [
// 'appId'=>$this->appId,//appid
// 'timestamp' =>$this->tretime(),//13位时间戳
// ];
// $server = '';
// // 签名
// $sign = $this->sort($data,$server);
// //参数体
// $date = [
// 'basicSetting'=>[
// 'name'=>'',//频道名
// 'channelPasswd'=>'',//频道密码
// 'categoryId'=>'',//分类ID 创建分类成功后会给你 categoryId
// //'maxViewer'=>'',//最大在线人数
// 'scene'=>'alone',//直播场景 alone活动场景
// 'startTime'=>$this->tretime(),//直播开始时间13位时间戳
// ],//频道详情
// 'sign'=>$sign,
// ];
// $data['sign'] = $sign;
// ksort($data);
// $url = 'http://api.polyv.net/live/v3/channel/basic/create';
// $result = $this->postJson($url,json_encode($date,320),http_build_query($data));
// $result = json_decode($result,true);
// print_r($result);die;
// }
// //创建直播分类
// public function create_zb(){
// //请求头
// $data = [
// 'appId'=>$this->appId,
// 'timestamp' =>$this->tretime(),
// 'categoryName'=>$title,//分类名
// ];
// $server = '';
// $data['sign'] = $this->sort($data,$server);
// ksort($data);
// $url = 'http://api.polyv.net/live/v3/user/category/create';
// $result = $this->postJson($url,'',http_build_query($data));
// return json_decode($result,true);
// }
//数据 server
protected function sort($data,$server){
ksort($data);
$string = '';
foreach ($data as $k=>$v){
$string .=$k.$v;
}
//拼接server
$string = $server.$string.$server;
//转大写 并md5加密
$string = strtoupper(md5($string));
return $string;
}
//13位毫秒级时间戳
protected function tretime(){
list($s1, $s2) = explode(' ', microtime());
$time = (float)sprintf('%.0f',(floatval($s1) + floatval($s2)) * 1000);
return $time;
}
/**
* http post请求
* @param $url
* @param $data
* @param array $header
* @param int $timeout
* @return mixed|string
* @throws Exception
*/
public function postJson($url, $data, $date,$header = array(
"Content-Type: application/json;charset=UTF-8"
),$timeout=5)
{
$url = trim($url.'?'.$date);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$res = curl_exec($ch);
if ($res === false) {
$res = curl_error($ch);
throw new Exception($res, 1);
}
curl_close($ch);
return $res;
}
public function setGet($url){
$curlobj = curl_init();
curl_setopt($curlobj, CURLOPT_URL, $url);
curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlobj, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curlobj, CURLOPT_SSL_VERIFYHOST, FALSE);
$output = curl_exec($curlobj);
curl_close($curlobj);
return $output;
}
//获取频道回放设置
public function playback($channel_id=''){
// 'http://api.polyv.net/live/v3/channel/playback/get-setting?appId=frlr1zazn3&sign=9DC1D9A3AAD34E0DD5C3B3154F1A6F5D&channelId=3187786×tamp=1655964947987'
$channel_id = I('channel_id');
$data = [
'appId'=>$this->appId,//appid
'channelId'=> $channel_id,//频道id
'timestamp' =>$this->tretime(),//13位时间戳
];
$server = $this->appsecret;
// 签名
$sign = $this->sort($data,$server);
$url = "http://api.polyv.net/live/v3/channel/playback/get-setting?appId=".$this->appId."&sign=".$sign."&channelId=".$channel_id."×tamp=".$data['timestamp'];
// echo $url;
$result = $this->setGet($url);
$arr = json_decode($result,true);
ll($arr);
}
//批量转存暂存视频到云点播
public function convert($channel_id='',$fileIds=''){
// 'http://api.polyv.net/live/v3/channel/record/convert'
// $channel_id = I('channel_id');
// $fileIds = I('fileIds');
$data = [
'appId'=>$this->appId,//appid
'timestamp' =>$this->tretime(),//13位时间戳
'channelId'=> $channel_id,//频道id
'fileIds' =>$fileIds,//要转存的录制视频文件ID,多个ID用英文逗号,分隔【fileId可通过查询频道录制视频信息获取
];
$server = $this->appsecret;
// 签名
$data['sign'] = $this->sort($data,$server);
$url = 'http://api.polyv.net/live/v3/channel/record/convert';
$result = $this->postJson($url,'',http_build_query($data));
$arr = json_decode($result,true);
return $arr;
}
//查询视频库列表
public function zb_list($channel_id=''){
// 'http://api.polyv.net/live/v2/channel/recordFile/3767243/playback/list?sign=726BEB4F4D43852EF7B5BEB469D86B02×tamp=1679364065979&channelId=3767243&appId=gia6b4afhv'
$channel_id = I('channel_id');
$pageSize = I('pageSize',10);
$page = I('page',1);
$data = [
'appId'=>$this->appId,//appid
'channelId'=> $channel_id,//频道id
'pageSize'=> $pageSize?$pageSize:10,//频道id
'page'=> $page?$page:1,//频道id
'timestamp' =>$this->tretime(),//13位时间戳
];
$server = $this->appsecret;
// 签名
$sign = $this->sort($data,$server);
$url = "http://api.polyv.net/live/v2/channel/recordFile/".$channel_id."/playback/list?sign=".$sign."×tamp=".$data['timestamp']."&channelId=".$channel_id."&appId=".$this->appId."&pageSize=".$pageSize."&page=".$page;
// echo $url;die;
$result = $this->setGet($url);
$arr = json_decode($result,true);
$this->ajaxReturn($arr);
}
//后台查询查询视频库列表
public function zb_list_over($channel_id=''){
$pageSize = I('pageSize',10);
$page = I('page',1);
$data = [
'appId'=>$this->appId,//appid
'channelId'=> $channel_id,//频道id
'pageSize'=> $pageSize?$pageSize:10,//频道id
'page'=> $page?$page:1,//频道id
'timestamp' =>$this->tretime(),//13位时间戳
];
$server = $this->appsecret;
// 签名
$sign = $this->sort($data,$server);
$url = "http://api.polyv.net/live/v2/channel/recordFile/".$channel_id."/playback/list?sign=".$sign."×tamp=".$data['timestamp']."&channelId=".$channel_id."&appId=".$this->appId."&pageSize=".$pageSize."&page=".$page;
$result = $this->setGet($url);
$arr = json_decode($result,true);
return $arr;
}
//平台回调接口方法
public function notify(){
$channelId = I('channelId');
$fileUrl = I('fileUrl');
$format = I('format');
$fileId = I('fileId');
//保存回放录屏
//查询最新课程
$first = M('class_child')->where(['channel_id'=>$channelId,'is_status'=>0])->order('start_time asc')->find();
//回放保存最新课程
$res = M('class_child')
->where(['id'=>$first['id']])
->save(['down_path'=>$fileUrl,'down_format'=>$format,'fileid'=>$fileId,'is_status'=>1]);
if($res){
$r = $this->convert($channelId,$fileId);//暂存转回放里
if($r['code']==200){
$c = M('class_child')->where(['channel_id'=>$channelId])->save(['msg'=>$r['data']]);//简单记录一下
}
echo 'SUCCESS';
}else{
echo 'ERROR';
}
// echo $fileUrl;
// echo $format;
// echo $channelId;die;
}
//直播间自定义url
public function diy_url(){
// echo 1;die;
$channelId = I('channelId');
$fileUrl = I('fileUrl');
$format = I('format');
$fileId = I('fileId');
//保存回放录屏
//查询最新课程
$first = M('class_child')->where(['channel_id'=>$channelId,'is_status'=>0])->order('start_time asc')->find();
//回放保存最新课程
$res = M('class_child')
->where(['id'=>$first['id']])
->save(['down_path'=>$fileUrl,'down_format'=>$format,'fileid'=>$fileId,'is_status'=>1]);
if($res){
$r = $this->convert($channelId,$fileId);//暂存转回放里
if($r['code']==200){
$c = M('class_child')->where(['channel_id'=>$channelId])->save(['msg'=>$r['data']]);//简单记录一下
}
echo 'SUCCESS';
}else{
echo 'ERROR';
}
// echo $fileUrl;
// echo $format;
// echo $channelId;die;
}
}
PHP对接保利威直播的屎山代码
于 2023-04-12 10:50:44 首次发布