tp5 创建直播频道

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/haibo0668/article/details/82996819

1、控制器

//									    	  //保利威
//									    	  $userId=checkRepeat('system_setup',67,1,'value',1);//保利威 userid
//									    	  $appId=checkRepeat('system_setup',68,1,'value',1);//保利威 appId
//									    	  $appSecret=checkRepeat('system_setup',69,1,'value',1);//保利威 appSecret
//									    	  //$rs_polyv4=checkRepeat('system_setup',70,1,'value',1);//保利威 appSecret备份
//									    	  
//									    	  if(!$userId || !$appId || !$appSecret){
//									    	  	   $this->error('错误:保利威直播参数没有填写!',url('system/live'));die;
//									    	  }
									    	  
										
												//接口需要的参数(非sign)赋值
												//签名验证必需参数
												$timestamp = time()*1000;
												$autoPlay = 1;//是否自动播放,0/1,默认1
												$name = input('title');
												$playerColor = "#00ffff";//播放器控制栏颜色,默认:#666666
												$channelPasswd = randomkeys(6);//频道密码
												$courseId = $rsid;//课程号 PID 课程ID
												$scene="alone";//直播场景:alone 活动拍摄 ppt 三分屏  topclass 大班课
												if($scene="alone"){
													$type2=0;
												}elseif($scene="ppt"){
													$type2=1;
												}elseif($scene="topclass"){
													$type2=2;
												}
												
												
												$params = array(
												    'appId'=>$appId,
												    //'appSecret'=>$appSecret,
												    'autoPlay'=>$autoPlay,
												    'name'=>$name,
												    'courseId'=>$courseId,
												    'playerColor'=>$playerColor,
												    'userId'=>$userId,
												    'channelPasswd'=>$channelPasswd,
												    'scene'=>$scene,
												    'timestamp'=>$timestamp
												  );
												
												//生成sign
												$sign = getSign($params,$appSecret); //$appSecret 这个参数不能放数组中 详细查看config.php文件的getSign方法
												
												$data = array(
												'appId' => $appId,
												'autoPlay'=>$autoPlay,
												'name'=>$name,
												'courseId'=>$courseId,
												'playerColor'=>$playerColor,
												'timestamp'=>$timestamp,
												'userId'=>$userId,
												'channelPasswd'=>$channelPasswd,
												'scene'=>$scene,
												'sign'=>$sign
												);
												
												$url = "http://api.polyv.net/live/v2/channels";
												$ch = curl_init() or die ( curl_error() );
												curl_setopt( $ch, CURLOPT_URL, $url);
												curl_setopt( $ch, CURLOPT_POSTFIELDS, $data);
												curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
												curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 360);
												$response = curl_exec ( $ch );
												curl_close ( $ch );
												
												//echo $response;
												$arr = json_decode($response, true);
												if($arr['code']==200){//=200成功
													
															$data3['type']=1;//状态 0短时间直播 1长时间直播
															$data3['type1']=$autoPlay;//是否自动播放,0/1,默认1
															$data3['type2']=$type2;// 直播场景:alone 活动拍摄 ppt 三分屏  topclass 大班课
															$data3['pid']=$rsid;
															
													     $data3['field1']=$arr['data']['channelId'];//直播频道ID
													     $data3['field2']=$channelPasswd;//频道密码
														 $data3['field3']=$arr['data']['userId'];//直播用户ID
														 $data3['field4']=$arr['data']['url'];//直播推流地址
														 $data3['field5']=$arr['data']['stream'];//直播流名称
														 $data3['field6']=$arr['data']['m3u8Url'];//直播拉流(播放)m3u8地址
														 $data3['field7']=$arr['data']['currentTimeMillis'];//务器返回的时间戳(毫秒)
																 
														$rsi=db('live_course_confing')->insert($data3);
														if(!$rsi){//统计成功条数
															 $e_info1 =";创建直播记录失败!";	
														}

												}else{
													$e_info1 =" ;创建直播失败!错误代码:".$arr['code'].",原因:".$arr['status'].$arr['message'];	
												}

2、getSign 签名生成规则

/**
 * 保利威云直播专用
 * 签名验证必需参数
 * $params 参数数组
 * $appSecret 这个参数不能放数组中
 * /获取sign函数
 */
function getSign($params,$appSecret){
    //global $appSecret;
    // 1. 对加密数组进行字典排序
    foreach ($params as $key=>$value){
     $arr[$key] = $key;
    }
    sort($arr);
    $str = $appSecret;
    foreach ($arr as $k => $v) {
     $str = $str.$arr[$k].$params[$v];
    }
    $restr = $str.$appSecret;
    $sign = strtoupper(md5($restr));
    return $sign;
}

官方文件:http://dev.polyv.net/2017/liveproduct/l-api/zbglgn/create-channel/

猜你喜欢

转载自blog.csdn.net/haibo0668/article/details/82996819
tp5