php對接網易雲信視頻直播


<?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));
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM