欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php对接网易云信视频直播

程序员文章站 2022-04-28 15:48:37
<?php
/**
* created by phpstorm.
* user: lhl
* date: 2019/4/10
* time: 17:31
*/

namespace app\api\controller;


class video
{
private $nonce;
private $curtime;
private $checksum;
const hex_digits = "0123456789abcdef";
public function __construct()
{
$this->appkey = '自己网易云信的appkey';
$this->appsecret = '自己网易云信的appsecret';
}

/**生成验证码**/
public
function checksumbuilder()
{
//此部分生成随机字符串
$hex_digits = self::hex_digits;
$this->nonce;
for ($i = 0; $i < 128; $i++) {
//随机字符串最大128个字符,也可以小于该数
$this->nonce .= $hex_digits[rand(0, 15)];
}
$this->curtime = (string)(time());//当前时间戳,以秒为单位
$join_string = $this->appsecret . $this->nonce . $this->curtime;
$this->checksum = sha1($join_string);
}

/*****file_get_contents()post请求******/
public
function postdatacurl($url = 'https://vcloud.163.com/app/channellist', $data = array())
{
$this->checksumbuilder();//发送请求前需先生成checksum

if (!empty($data)) {
$data = json_encode($data);
} else {
$data = "";
}

$options = array(
'http' => array(
'method' => 'post',
'header' => "content-type: application/json;charset=utf-8\r\n" . "appkey:" . $this->appkey . "\r\n" . "nonce:" . $this->nonce . "\r\n" .
"curtime:" . $this->curtime . "\r\n" . "checksum:" . $this->checksum . "",
'content' => $data,
'timeout' => 500,
)
);
$context = stream_context_create($options);

$result = file_get_contents($url, false, $context);

return json_decode($result, true);
}


/***创建频道***/
public function channel_add($name, $type = 0)
{
$url = "https://vcloud.163.com/app/channel/create";
$data = $this->postdatacurl($url, array("name" => $name, "type" => $type));
// var_dump($data);
return json($data);
}


/****修改频道*****/
public function channel_update($name, $cid, $type = 0)
{
$url = "https://vcloud.163.com/app/channel/update";
return $data = $this->postdatacurl($url, array("name" => $name, "cid" => $cid, "type" => $type));
}


/****删除频道******/
public function channel_delete($cid)
{
$url = "https://vcloud.163.com/app/channel/delete";
return $data = $this->postdatacurl($url, array("cid" => $cid));
}


/****获取频道状态******/
public function channel_get($cid)
{
$url = "https://vcloud.163.com/app/channelstats";
return $data = $this->postdatacurl($url, array("cid" => $cid));
}


/***
*     获取频道列表
*     records int 单页记录数,默认值为10    否
*     pnum    int 要取第几页,默认值为1 否
*     ofield  string  排序的域,支持的排序域为:ctime(默认)  否
*     sort    int 升序还是降序,1升序,0降序,默认为desc  否
*     **/
public function channel_list($option = array("records" => 10, "pnum" => 1, "ofield" => "ctime", "sort" => 1))
{
$url = "https://vcloud.163.com/app/channellist";
return $data = $this->postdatacurl($url, $option);
}


/**重新获取推流地址***/
public function channel_reset($cid)
{
$url = "https://vcloud.163.com/app/address";
return $data = $this->postdatacurl($url, array("cid" => $cid));
}


/*****
*     设置频道为录制状态
*     cid string  频道id    是
*     needrecord  int 1-开启录制; 0-关闭录制  是
*     format  int 1-flv; 0-mp4    是
*     duration    int 录制切片时长(分钟),默认120分钟  否
*     filename    string  录制后文件名,格式为filename_yyyymmdd-hhmmssyyyymmdd-hhmmss, 
*     文件名录制起始时间(年月日时分秒) -录制结束时间(年月日时分秒)   否
*     ****/
public function channel_setrecord($cid, $option = array())
{
$url = "https://vcloud.163.com/app/channel/setalwaysrecord";
return $data = $this->postdatacurl($url, $option);
}


/****禁用频道*****/
public function channel_pause($cid)
{
$url = "https://vcloud.163.com/app/channel/pause";
return $data = $this->postdatacurl($url, array("cid" => $cid));
}


/****批量禁用频道****/
public function channel_pauselist($cidlist)
{
$url = "https://vcloud.163.com/app/channellist/pause";
return $data = $this->postdatacurl($url, array("cidlist" => $cidlist));
}

/****恢复频道*****/
public function channel_resume($cid)
{
$url = "https://vcloud.163.com/app/channel/resume";
return $data = $this->postdatacurl($url, array("cid" => $cid));
}


/****批量恢复频道****/
public function channel_resumelist($cidlist)
{
$url = "https://vcloud.163.com/app/channellist/resume";
return $data = $this->postdatacurl($url, array("cidlist" => $cidlist));
}


/****获取录制视频文件列表*****/
public function channel_videolist($cid)
{
$url = "https://vcloud.163.com/app/videolist";
return $data = $this->postdatacurl($url, array("cid" => $cid));
}

/****获取某一时间范围的录制视频文件列表*****/
public function app_vodvideolist($cid, $begintime, $endtime)
{
$url = "https://vcloud.163.com/app/vodvideolist";
return $data = $this->postdatacurl($url, array("cid" => $cid, "begintime" => $begintime, "endtime" => $endtime));
}


/****设置视频录制回调地址*****/
public function record_setcallback($recordclk)
{
$url = "https://vcloud.163.com/app/record/setcallback";
return $data = $this->postdatacurl($url, array("recordclk" => $recordclk));
}


/****设置回调的加签秘钥*****/
public function callback_setsignkey($signkey)
{
$url = "https://vcloud.163.com/app/callback/setsignkey";
return $data = $this->postdatacurl($url, array("signkey" => $signkey));
}
/****录制文件合并*****/
public function video_merge($outputname, $vidlist)
{
$url = "https://vcloud.163.com/app/video/merge";
return $data = $this->postdatacurl($url, array("outputname" => $outputname, "vidlist" => $vidlist));
}
}