我是靠谱客的博主 呆萌金鱼,最近开发中收集的这篇文章主要介绍php--tp5萤石摄像头接口,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

php代码

//摄像头添加
public function add()
{
    if(request()->isAjax())
    {
        if(!trim(input('name')))
        {
            return $this->msg(false,'请输入设备名称');
        }
        $token = $this->token();
        if(input('id'))
        {
        	//如果设备存在,只修改设备名称
            $data = YingshiModel::find(input('id'));
            $name = $this->curl_post('https://open.ys7.com/api/lapp/device/name/update',['accessToken'=>$token,'deviceSerial'=>$data['num'],'deviceName'=>input('name')],false);
        }else{
        	//如果设备不存在
            if(!trim(input('num'))){
                return $this->msg(false,'请输入设备序列号');
            }
            if(!trim(input('code')))
            {
                return $this->msg(false,'请输入设备验证码');
            }
            $data = new YingshiModel();
            $data->createtime = time();
            //添加设备
            $add = $this->curl_post('https://open.ys7.com/api/lapp/device/add',['accessToken'=>$token,'deviceSerial'=>input('num'),'validateCode'=>input('code')],false);
            $add = json_decode($add,true);
            if($add['code'] != 200 && $add['code'] != 20017)
            {
                return $this->msg(false,$add['msg']);
            }

            //关闭设备加密
            $jiami = $this->curl_post('https://open.ys7.com/api/lapp/device/encrypt/off',['accessToken'=>$token,'deviceSerial'=>input('num'),'validateCode'=>input('code')],false);
            $jiami = json_decode($jiami,true);
            if($jiami['code'] != 200 && $jiami['code'] != 60016)
            {
                return $this->msg(false,$jiami['msg']);
            }
            //关闭麦克风
            $m = $this->curl_post('https://open.ys7.com/api/lapp/camera/video/sound/set',['accessToken'=>$token,'deviceSerial'=>input('num'),'enable'=>0],false);
            $m = json_decode($m,true);
            if($m['code'] != 200)
            {
                return $this->msg(false,$m['msg']);
            }
            //开通直播
            $zhibo = $this->curl_post('https://open.ys7.com/api/lapp/live/video/open',['accessToken'=>$token,'source'=>input('num').':1'],false);
            $zhibo = json_decode($zhibo,true);
            if($zhibo['code'] != 200)
            {
                return $this->msg(false,$zhibo['msg']);
            }
            //获取直播地址
            $dizhi = $this->curl_post('https://open.ys7.com/api/lapp/live/address/limited',['accessToken'=>$token,'deviceSerial'=>input('num'),'channelNo'=>1],false);
            $dizhi = json_decode($dizhi,true);
            if($dizhi['code'] != 200)
            {
                return $this->msg(false,$dizhi['msg']);
            }
            $data->url = $dizhi['data']['liveAddress'];
            //开启设备下线通知
            $tongzhi = $this->curl_post('https://open.ys7.com/api/lapp/device/notify/switch',['accessToken'=>$token,'deviceSerial'=>input('num'),'enable'=>1],false);
            $tongzhi = json_decode($tongzhi,true);
            if($tongzhi['code'] != 200)
            {
                return $this->msg(false,$tongzhi['msg']);
            }
            //修改设备名称
            $name = $this->curl_post('https://open.ys7.com/api/lapp/device/name/update',['accessToken'=>$token,'deviceSerial'=>input('num'),'deviceName'=>input('name')],false);
            $data->code = input('code');
            $data->name = input('name');
        }
        $name = json_decode($name,true);
        if($name['code'] != 200)
        {
            return $this->msg(false,$name['msg']);
        }
        $data->num = input('num');
        $data->member_id = input('member_id');
        $data->shop_id = input('shop_id');
        $s = $data->save();
        if($s)
        {
            return $this->msg(true,'操作成功');
        }else{
            return $this->msg(false,'操作失败');
        }
    }
    if(input('id'))
    {
        $data = YingshiModel::find(input('id'));
        $shops = Shop::where('id',$data['shop_id'])->find();
        $this->assign('shops',$shops);
        if($data['member_id'])
        {
            $member = Member::where('id',$data['member_id'])->find();
            $this->assign('member',$member);
        }
        $this->assign('data',$data);
    }
    $shop = Shop::select();
    $this->assign('shop',$shop);
    return $this->fetch();
}
//获取token
public function token()
{
    $set = Yingshiset::find();
    //萤石官方的token只保存7天
    if(!$set['token'] || $set['endtime'] < time())
    {
    	//本地保存的token已经保存超过5天,更新token
        $token = $this->curl_post('https://open.ys7.com/api/lapp/token/get',['appKey'=>$set['appid'],'appSecret'=>$set['appkey']],false);
        $token = json_decode($token,true);
        $set->token = $token['data']['accessToken'];
        $set->endtime = time()+3600 *24 *5;
        $set->save();
    }
    return $set['token'];
}

//删除设备
public function del()
{
    $id = input('id');
    $data = YingshiModel::find($id);
    $token = $this->token();
    $s = $this->curl_post('https://open.ys7.com/api/lapp/device/delete',['accessToken'=>$token,'deviceSerial'=>$data['num']],false);
    $s = json_decode($s,true);
    if($s['code'] != 200)
    {
        return $this->msg(false,$s['msg']);
    }else{
        $s= $data->delete();
        if($s)
        {
            return $this->msg(true,'删除成功');
        }else{
            return $this->msg(false,'删除失败');
        }
    }
}
protected function curl_post($url,$post_data,$isTrue = true)
{
    if ($isTrue) {
        $post_data = json_encode($post_data);
    }
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL, $url);
    if(stripos($url,"https://")!==FALSE){
        curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
        // curl_setopt($ch, CURLOPT_SSL_VERIFYHOSTIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    }    else    {
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,TRUE);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);//严格校验
    }
    //设置header
    curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    //要求结果为字符串且输出到屏幕上
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    //设置超时
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    //传输文件
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    //运行curl
    $data = curl_exec($ch);
    //返回结果
    curl_close($ch);
    return $data;
}

最后

以上就是呆萌金鱼为你收集整理的php--tp5萤石摄像头接口的全部内容,希望文章能够帮你解决php--tp5萤石摄像头接口所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部