| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473 |
- <?php
- /**
- * Created by PhpStorm.
- * User: ellisran
- * Date: 2016/9/21
- * Time: 14:41
- */
- class video {
- private $AppKey = 'f277356253794d16bbef1f2107c3e84e'; //开发者平台分配的AppKey
- private $AppSecret = 'dbb0e89f0e9943b5b7e837731a8deacd'; //开发者平台分配的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";
- private $baseUrl = 'https://vcloud.163.com'; //网易上传地址
- /**
- * 析构函数
- * @param $AppKey 网易视频云提供的key
- * @param $AppSecret 网易视频云提供的AppSecret
- */
- function __construct() {
- }
- // 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) ;
- }
- //start 视频分类管理
- /**
- * 获取视频分类列表信息
- * @currentPage 获取视频分类列表分页后的索引
- * @pageSize 获取视频分类列表一页的记录数(若为-1,表示不用分页)
- */
- public function videoTypeList($currentPage, $pageSize){
- $url = $this->baseUrl.'/app/vod/type/list';
- return $this->postDataCurl($url,array('currentPage' => $currentPage, 'pageSize' => $pageSize));
- }
- /**
- * 创建视频分类
- * @typeName 视频分类的名称
- * @description 视频分类的描述信息
- */
- public function videoTypeCreate($typeName, $description = ''){
- $url = $this->baseUrl.'/app/vod/type/create';
- if($description != ''){
- return $this->postDataCurl($url,array('typeName' => $typeName, 'description' => $description));
- }else{
- return $this->postDataCurl($url,array('typeName' => $typeName));
- }
- }
- /**
- * 获取视频分类信息
- * @typeId 视频分类的Id
- */
- public function videoTypeGet($typeId){
- $url = $this->baseUrl.'/app/vod/type/get';
- return $this->postDataCurl($url,array('typeId' => $typeId));
- }
- /**
- * 修改视频分类信息
- * @typeId 视频分类的Id
- * @typeName 视频分类的名称
- * @description 视频分类的描述信息
- */
- public function videoTypeUpdate($typeId, $typeName, $description = ''){
- $url = $this->baseUrl.'/app/vod/type/update';
- if($description != ''){
- return $this->postDataCurl($url,array('typeId' => $typeId, 'typeName' => $typeName, 'description' => $description));
- }else{
- return $this->postDataCurl($url,array('typeId' => $typeId, 'typeName' => $typeName));
- }
- }
- /**
- * 删除视频分类
- * @typeId 视频分类的Id
- */
- public function videoTypeDelete($typeId){
- $url = $this->baseUrl.'/app/vod/type/typeDelete';
- return $this->postDataCurl($url,array('typeId' => $typeId));
- }
- //end 视频分类管理
- //start 视频管理
- /**
- * 获取视频文件信息
- * @vid 视频Id
- */
- public function videoGet($vid){
- $url = $this->baseUrl.'/app/vod/video/get';
- return $this->postDataCurl($url,array('vid' => $vid));
- }
- /**
- * 获取视频文件信息列表
- * @currentPage 获取视频列表分页后的索引
- * @pageSize 获取视频列表一页的记录数(若为-1,表示不用分页)
- * @statys 根据视频状态过滤选择(0表示获取所有状态视频,10表示初始,20表示失败,30表示处理中,40表示成功,50表示屏蔽)
- * @typeId 根据视频分类过滤选择(0表示获取所有分类视频)
- */
- public function videoList($currentPage, $pageSize, $status, $type){
- $url = $this->baseUrl.'/app/vod/video/list';
- return $this->postDataCurl($url,array('currentPage' => $currentPage, 'pageSize' => $pageSize, 'status' => $status, 'type' => $type));
- }
- /**
- * 修改视频文件信息
- * @vid 视频Id
- * @videoName 视频的名称
- * @typeId 视频分类Id
- * @description 视频的描述信息
- */
- public function videoEdit($vid, $videoName, $typeId, $description = ''){
- $url = $this->baseUrl.'/app/vod/video/edit';
- if($description != ''){
- return $this->postDataCurl($url,array('vid' => $vid, 'videoName' => $videoName, 'typeId' => $typeId, 'description' => $description));
- }else{
- return $this->postDataCurl($url,array('vid' => $vid, 'videoName' => $videoName, 'typeId' => $typeId));
- }
- }
- /**
- * 删除视频文件
- * @vid 视频Id
- */
- public function videoDetele($vid){
- $url = $this->baseUrl.'/app/vod/video/videoDelete';
- return $this->postDataCurl($url,array('vid' => $vid));
- }
- //end 视频管理
- //start 视频上传管理
- /**
- * 检测文件编码
- * @param string $file 文件路径
- * @return string|null 返回 编码名 或 null
- */
- function detect_encoding($file) {
- $list = array('GBK', 'UTF-8', 'UTF-16LE', 'UTF-16BE', 'ISO-8859-1');
- $str = file_get_contents($file);
- foreach ($list as $item) {
- $tmp = mb_convert_encoding($str, $item, $item);
- if (md5($tmp) == md5($str)) {
- return $item;
- }
- }
- return null;
- }
- /**
- * get curl 方法
- * 断点续传查询断点,获取上传加速节点地址 使用本方法
- */
- public function GetUrlMsg($url, $xNosToken = ''){
- $ch = curl_init();
- curl_setopt ($ch, CURLOPT_URL, $url);
- if($xNosToken != ''){
- $header = array(
- 'x-nos-token:'. $xNosToken
- );
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- }
- curl_setopt ($ch, CURLOPT_HEADER, false);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $result = curl_exec($ch);
- if (false === $result) {
- $result = curl_errno($ch);
- }
- curl_close($ch);
- return json_decode($result,true);
- }
- /**
- * 上传视频初始化
- * @originFileName 上传文件的原始名称(包含后缀名)(必填)
- * @userFileName 用户命名的上传文件名称
- * @typeId 视频所属的类别Id(不填写为默认分类)
- */
- public function videoInit($oname, $uname = '', $type = ''){
- $url = $this->baseUrl.'/app/vod/upload/init';
- return $this->postDataCurl($url,array('originFileName' => $oname, 'userFileName' => $uname, 'typeId' => $type));
- }
- /**
- * 获取上传加速节点地址
- * @version API版本号,填写固定值1.0
- * @bucketname 存储上传文件的桶名,可在视频上传初始化接口的返回参数bucket获取
- */
- public function videoLbsGet($bucketname){
- $url = 'http://wanproxy.127.net/lbs?version=1.0&bucketname='.$bucketname;
- return $this->GetUrlMsg($url);
- }
- /**
- * 上传视频
- * @url 上传地址格式:POST {UploadHost}/{bucket}/{object}?offset=0&complete=false&version=1.0
- {UploadHost}值为获取的上传加速节点地址,{bucket}值为存储对象的桶名,{object}值为生成的唯一对象名。
- * @file 服务器缓存文件
- *
- * 请求参数
- * @x-nos-token 请求头参数,上传token
- * @bucket 存储对象的桶名
- * @object 生成的唯一对象名
- * @offset 当前分片在整个对象中的起始偏移量,单位:字节(Byte)
- * @complete 是否为最后一块数据。合法值:true/false
- * @version http api版本号。这里是固定值1.0
- * @context 上传上下文。本字段是只能被上传服务器解读使用的不透明字段,
- 上传端不应修改其内容。
- 注意:用户第一次上传应不带此参数或置为空字符串,
- 之后上传剩余部分数据都需要带上这个参数。
- context对应的桶名或者对象名不匹配返回400 code
- *
- * 响应参数(返回结果)
- * @requestId uuid字符串,服务器端生成的唯一UUID
- * @offset 下一个上传片在上传块中的偏移。
- 注意:偏移从0开始,比如:用户上传0-128字节后,
- 服务器返回的offset为128,下一次上传offset值应置为128
- * @context 上传上下文
- * @callbackRetMsg 上传回调信息
- */
- public function videoUpload($url,$token,$file){
- // return $this->sendStreamFile($url,$token,$file);
- // if(file_exists($file)){
- // $filesize=filesize($file);
- // $handle = fopen ($file , "rb" );
- // $contents = fread ( $handle , $filesize);
- //// var_dump($this->detect_encoding($file));
- //// $contents = mb_convert_encoding($contents ,'utf-8', 'ISO-8859-1');
- // fclose ( $handle );
- // x-www-form-urlencoded
- // $header = array(
- // 'Content-Type:application/json;charset=utf-8',
- // 'Content-Length:'.strlen($file),
- // 'x-nos-token:'.$token,
- // 'Content-MD5:'.md5($file)
- //// 'Access-Control-Allow-Origin: *'
- // );
- $headers = array('x-nos-token' => $token,'Content-type'=>'application/json;charset=UTF-8');
- $t1 = microtime(true);
- $ch = curl_init();
- $options = array(
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_SSL_VERIFYPEER => false,
- CURLOPT_SSL_VERIFYHOST => false,
- CURLOPT_HEADER => true,
- CURLOPT_NOBODY => false,
- CURLOPT_CUSTOMREQUEST => 'POST',
- CURLOPT_URL => $url
- );
- // curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- // curl_setopt($ch, CURLOPT_HEADER, true);
- // curl_setopt($ch, CURLOPT_NOBODY, false);
- // curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
- // curl_setopt($ch, CURLOPT_URL, $url);
- // Handle open_basedir & safe mode
- if (!ini_get('safe_mode') && !ini_get('open_basedir')) {
- $options[CURLOPT_FOLLOWLOCATION] = true;
- }
- $header = array();
- foreach ($headers as $key => $val) {
- array_push($header, "$key: $val");
- }
- $options[CURLOPT_HTTPHEADER] = $header;
- curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
- $options[CURLOPT_POSTFIELDS] = $file;
- curl_setopt_array($ch, $options);
- // curl_setopt($ch, CURLOPT_POSTFIELDS, $file);
- // curl_getinfo($ch);
- $return_data = curl_exec($ch);
- // curl_close($ch);
- $t2 = microtime(true);
- $duration = round($t2-$t1, 3);
- $ret = curl_errno($ch);
- if ($ret !== 0) {
- $r = new Response(-1, $duration, array(), null, curl_error($ch));
- curl_close($ch);
- return $r;
- }
- $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
- $headers = self::parseHeaders(substr($return_data, 0, $header_size));
- $body = substr($return_data, $header_size);
- curl_close($ch);
- // var_dump($return_data);
- // var_dump($code);
- // var_dump($duration);
- // var_dump($headers);
- var_dump($body);
- // exit;
- return json_decode($return_data,true);
- // $opts = array(
- // 'http' => array(
- // 'method' => 'POST',
- // 'header' => array("Content-Type: application/x-www-form-urlencoded;charset=utf-8","x-nos-token:".$token,"Content-Length:".strlen($contents)),
- // 'content' => $contents
- // )
- // );
- // $context = stream_context_create($opts);
- // $response = file_get_contents($url, false, $context);
- // $ret = json_decode($response, true);
- //// return $ret['success'];
- //// var_dump($ret);
- //// exit;
- // return $ret;
- // }else{
- // return false;
- // }
- }
- private static function parseHeaders($raw)
- {
- $headers = array();
- $headerLines = explode("\r\n", $raw);
- foreach ($headerLines as $line) {
- $headerLine = trim($line);
- $kv = explode(':', $headerLine);
- if (count($kv) >1) {
- $headers[$kv[0]] = trim($kv[1]);
- }
- }
- return $headers;
- }
- /**
- * 断点续传查询断点
- * @url 上传地址格式:GET {UploadHost}/{bucket}/{object}?uploadContext
- *
- * 请求参数
- * @x-nos-token 请求头参数,上传token
- * @bucket 存储对象的桶名
- * @object 生成的唯一对象名
- * @context 上传上下文。本字段是只能被上传服务器解读使用的不透明字段,
- 上传端不应修改其内容。对应context在服务端不存在则返回404。
- context对应的桶名或者对象名不匹配返回400 code。
- * @version http api版本号。这里是固定值1.0
- */
- public function videoUploadContext($url,$token,$context){
- $url = $url.'?uploadContext&context='.$context.'&version=1.0';
- return $this->GetUrlMsg($url,$token);
- }
- /**
- * 视频上传完成查询视频主id,存在id则表示上传完成
- * @objectNames 上传文件的对象名列表
- *
- */
- public function videoQuery($objectName){
- $url = $this->baseUrl.'/app/vod/video/query';
- return $this->postDataCurl($url,array('objectNames' => array($objectName)));
- }
- /**
- * 设置上传回调地址接口 用于视频上传,设置上传成功后的回调地址。
- * @callbackUrl 上传成功后回调客户端的URL地址
- *
- */
- public function videoCallback($cburl){
- $url = $this->baseUrl.'/app/vod/upload/setcallback';
- return $this->postDataCurl($url,array('callbackUrl' => $cburl));
- }
- /**
- * 获取视频封面
- * @id 视频Id
- * @size 截图尺寸,包含以下几种值:1表示640X360,2表示400X300,3表示320X180,4表示200X150
- * @offset 截图偏移,包含以下几种值:
- 0表示视频第一帧,1表示时间轴10%位置,2表示时间轴20%位置,3表示时间轴30%位置,
- 4表示时间轴40%位置,5表示时间轴50%位置,6表示时间轴60%位置,
- 7表示时间轴70%位置,8表示时间轴80%位置,9表示时间轴90%位置
- */
- public function videoSnapshotCreate($id, $size, $offset){
- $url = $this->baseUrl.'/app/vod/snapshot/create';
- return $this->postDataCurl($url,array('vid' => $id, 'size' => $size, 'offset' => $offset));
- }
- /**
- * 设置视频封面
- * @id 视频Id
- * @typeId 封面设置方法:1表示使用截图URL,2表示使用本地上传图片
- * @path type值为1,则代表截图URL;type值为2,则代表本地图片路径
- * @data type值为2时,需填写,代表本地图片数据的base64编码字符串数据
- */
- public function videoSnapshotSet($id, $typeId, $path, $data = ''){
- $url = $this->baseUrl.'/app/vod/snapshot/set';
- if($data != ''){
- return $this->postDataCurl($url,array('vid' => $id, 'type' => $typeId, 'path' => $path, 'data' => $data));
- }else{
- return $this->postDataCurl($url,array('vid' => $id, 'type' => $typeId, 'path' => $path));
- }
- }
- //end 视频上传管理
- }
- ?>
|