<meta charset="utf-8">
<?php
//获取sign函数
//function getSign($params){
// //global $appSecret;
// // 1. 对加密数组进行字典排序
// foreach ($params as $key=>$value){
// $arr[$key] = $key;
// }
// sort($arr);
// $str = $params['appSecret'];
// foreach ($arr as $k => $v) {
// $str = $str.$arr[$k].$params[$v];
// }
// $restr = $str.$params['appSecret'];
// $sign = strtoupper(md5($restr));
// return $sign;
//}
//引用config.php
//include 'config.php';
//签名验证必需参数
$appId = "ezhjgj77wv0";
//$timestamp = time()*1000;
//var_dump($timestamp);die;
$appSecret = "5ebbf5j60fdgd54b3873d5d1";
//接口需要的参数(非sign)赋值
$userId = "aed787hgja85";
$autoPlay = 1;
$playerColor = "#00ffff";
$channelPasswd = "123456";
$courseId = "123456";
$scene = "alone";
$url = "http://api.polyv.net/live/v2/channels";
$s_title2[]="内部测试01";
$s_title2[]="内部测试02";
$times[]="1532274383000";
$times[]="1532274395000";
//var_dump($times);die;
for($i=0;$i<count($s_title2);$i++){
$timestamp = time()*1000;
$name = $s_title2[$i];
$params = array(
'appId'=>$appId,
'autoPlay'=>$autoPlay,
'name'=>$name,
'courseId'=>$courseId,
'playerColor'=>$playerColor,
'userId'=>$userId,
'channelPasswd'=>$channelPasswd,
'scene'=>$scene,
'timestamp'=>$timestamp
);
foreach ($params as $key=>$value){
$arr2[$key] = $key;
}
//var_dump($arr2);
sort($arr2);
$str = $appSecret;
foreach ($arr2 as $k => $v) {
$str = $str.$arr2[$k].$params[$v];
}
$restr = $str.$appSecret;
//echo($restr."<br>");
$sign = strtoupper(md5($restr));
//echo($sign."<br>");
// return $sign;
$data = array(
'appId' => $appId,
'autoPlay'=>$autoPlay,
'name'=>$name,
'courseId'=>$courseId,
'playerColor'=>$playerColor,
'timestamp'=>$timestamp,
'userId'=>$userId,
'channelPasswd'=>$channelPasswd,
'scene'=>$scene,
'sign'=>$sign
);
$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 );
//取得返回数组
$arr = json_decode($response, true);
$code .= $arr['code'].",";//code=200 说明成功
$message=$arr['message'];
$restr ="";
$sign ="";
$arr2 ="";
$params[] ="";
}
echo $code;
?>
二、保利版本批量增加
批量创建频道
1、先组成数据
//组成数组
$channels2[$i]['name']=$title2[$i];
$channels2[$i]['courseId']=$pid;
$channels2[$i]['autoPlay']=1;
$channels2[$i]['playerColor']="#00ffff";//播放器控制栏颜色,默认:#666666
$channels2[$i]['scene']="alone";//直播场景:alone 活动拍摄 ppt 三分屏 topclass 大班课
$channels2[$i]['channelPasswd']=randomkeys(6);//频道密码
2、数组改成JSON数据
$channels['channels']=$channels2;
$json=json_encode($channels);
3、批量创建直播
$timestamp = time()*1000;
$params = array(
'appId' => $appId,
'timestamp' => $timestamp,
);
//生成sign
$sign = getSign($params,$appSecret); //$appSecret 这个参数不能放数组中 详细查看config.php文件的getSign方法
$channels['channels']=$channels2;
$json=json_encode($channels);
$params["sign"] = $sign;
$url="http://api.polyv.net/live/v3/channel/basic/batch-create?".http_build_query($params);
function post($url, $post_data = '', $timeout = 5){
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
if($post_data != ''){
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($post_data)));
$file_contents = curl_exec($ch);
curl_close($ch);
return $file_contents;
}
//echo post($url,$json);
$response=post($url,$json);
dump($response);die;