设为首页 加入收藏

TOP

php对接网易云信视频直播(一)
2019-08-15 23:33:08 】 浏览:74
Tags: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&nbs
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇如何利用HTTP缓存来加快你的网站.. 下一篇phpstorm 实现SFTP开发,线上线下..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目