php版網易視頻雲api

最近在作在線教育課程,使用網易雲視頻做爲在線視頻直播。php

網易官方只有java示例,咱們使用php,就本身寫個api。java

固然實現也是很簡單的。git

演示:http://www.deitui.com/index.php?m=openclass json

class v163Class{
	private $AppKey;                //開發者平臺分配的AppKey
    private $AppSecret;             //開發者平臺分配的AppSecret,可刷新
    private $Nonce;					//隨機數(最大長度128個字符)
    private $CurTime;             	//當前UTC時間戳,從1970年1月1日0點0 分0 秒開始到如今的秒數(String)
    private $CheckSum;				//SHA1(AppSecret + Nonce + CurTime),三個參數拼接的字符串,進行SHA1哈希計算,轉化成16進制字符(String,小寫)
    const   HEX_DIGITS = "0123456789abcdef";
	public function __construct($AppKey,$AppSecret){
        $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);
 
    }
 
	/*****post請求******/
	public function postDataCurl($url,$data=array()){
    	$this->checkSumBuilder();		//發送請求前需先生成checkSum
		if(!empty($data)){
			$json=json_encode($data);
		}else{
			$json="";
		}
		$timeout = 5000;  
        $http_header = array(
            'AppKey:'.$this->AppKey,
            'Nonce:'.$this->Nonce,
            'CurTime:'.$this->CurTime,
            'CheckSum:'.$this->CheckSum,
            'Content-Type: application/json;charset=utf-8;',
			'Content-Length: ' . strlen($json)
        );
		$ch = curl_init(); 
		curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt ($ch, CURLOPT_POST, 1);
        curl_setopt ($ch, CURLOPT_POSTFIELDS, $json);
        curl_setopt ($ch, CURLOPT_HEADER, false); 
		curl_setopt ($ch, CURLOPT_HTTPHEADER,$http_header);
		curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        
		$result = curl_exec($ch);  
        if (false === $result) {
            $result =  curl_errno($ch);
        }
		curl_close($ch); 
		return json_decode($result,true) ;
    } 
	/***頻道添加***/
	public function channel_add($name,$type=0){
		$url="https://vcloud.163.com/app/channel/create";
		return $data=$this->postDataCurl($url,array("name"=>$name,"type"=>$type));
	}
	/****頻道更新*****/
	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));
	}
	
	
}
相關文章
相關標籤/搜索