php 接入海康平台

2023-05-16

php获取海康平台的监控流地址
先获取所有监控点(/artemis/api/resource/v1/cameras)在根据监控点的cameraIndexCode请求/artemis/api/video/v1/cameras/previewURLs接口获取监控的视频流,
备注:h5播放的话获取视频流时取流协议protocol 要选择 hls,播放时要引入video.js

<?php
namespace app\controller;

use app\BaseController;
class Haikang extends BaseController
{
    public $pre_url = "https://**.***.***";
    protected $app_key = "***"; 
    protected $app_secret = "****";

    public $time; //时间戳
    public $content_type = "application/json"; //json类型
    public $accept = "*/*";
    public $method = array(
        "POST" => "POST"
    );

    public $list_url = array(
        'resource/v1/cameras' => "/artemis/api/resource/v1/cameras",
        'online/camera/get' => "/artemis/api/nms/v1/online/camera/get",
        'vqd/list' => "/artemis/api/nms/v1/vqd/list",
        'record/list' => "/artemis/api/nms/v1/record/list",
        'region/nodesByParams' => "/artemis/api/irds/v2/region/nodesByParams",
        'regions/regionIndexCode/cameras' => "/artemis/api/resource/v1/regions/regionIndexCode/cameras",
        'cameras/previewURLs' => "/artemis/api/video/v1/cameras/previewURLs",
        'regions/camera/search' =>'/artemis//api/resource/v2/camera/search'
    );

    public $date = null;
    public function __construct($app_key = '', $app_secret = '')
    {
        session_start();
        if ($app_key != '') $this->app_key = $app_key;
        if ($app_secret != '') $this->app_secret = $app_secret;
        $this->charset = 'utf-8';
        list($msec, $sec) = explode(' ', microtime());
        $this->time = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
    }
    // 分页获取监控点资源
    public function get_camera()
    {
        //请求参数
        $postData = [
            "pageNo" => "1",
            "pageSize" => "500",
        ];

        $sign = $this->get_sign($postData, $this->list_url['resource/v1/cameras']);
        $options = array(
            CURLOPT_HTTPHEADER => array(
                "HTTP METHOD:" . $this->method['POST'],
                "Accept:" . $this->accept,
                "Content-Type:" . $this->content_type,
                "Date:" . $this->get_date(),
                "x-Ca-Key:" . $this->app_key,
                "X-Ca-Signature:" . $sign,
                "X-Ca-Signature-Headers: x-ca-key",
            )
        );
        $result1 = $this->curlPost($this->pre_url . $this->list_url['resource/v1/cameras'], json_encode($postData), $options);
        // dump($result1);die;
        // return $result1;
        return json_decode($result1, true);
    }
    // 获取监控点在线状态
    public function get_online_camera($includeSubNode = null)
    {
        //请求参数
        if ($includeSubNode) {
            $postData = [
                "pageNo" => 1,
                "pageSize" => 300,
                "includeSubNode" => $includeSubNode,
            ];
        } else {
            $postData = [
                "pageNo" => 1,
                "pageSize" => 500,
            ];
        }

        $sign = $this->get_sign($postData, $this->list_url['online/camera/get']);
        $options = array(
            CURLOPT_HTTPHEADER => array(
                "HTTP METHOD:" . $this->method['POST'],
                "Accept:" . $this->accept,
                "Content-Type:" . $this->content_type,
                "Date:" . $this->get_date(),
                "x-Ca-Key:" . $this->app_key,
                "X-Ca-Signature:" . $sign,
                "X-Ca-Signature-Headers: x-ca-key",
            )
        );
        $result1 = $this->curlPost($this->pre_url . $this->list_url['online/camera/get'], json_encode($postData), $options);

        return json_decode($result1, true);
    }
    // 获取监控点预览取流URLv2
    public function get_previewURLs()
    {
        $cameraIndexCode = input('cameraIndexCode');
        if(!$cameraIndexCode)
        {
            return "cameraIndexCode不能为空";
        }
        //请求参数  eef72184562f42cd9df5d9030adca01d cf5ddb99469844bf8e43c0a62ecb8708 6f037f787a2e47c188cbb6e6d42d2b83
        $postData = [
            "cameraIndexCode" => $cameraIndexCode,
            "streamType"=> 1,
            "protocol"=> "hls",
            "transmode"=> 1
        ];

        $sign = $this->get_sign($postData, $this->list_url['cameras/previewURLs']);
        $options = array(
            CURLOPT_HTTPHEADER => array(
                "HTTP METHOD:" . $this->method['POST'],
                "Accept:" . $this->accept,
                "Content-Type:" . $this->content_type,
                "Date:" . $this->get_date(),
                "x-Ca-Key:" . $this->app_key,
                "X-Ca-Signature:" . $sign,
                "X-Ca-Signature-Headers: x-ca-key",
            )
        );
        $result1 = $this->curlPost($this->pre_url . $this->list_url['cameras/previewURLs'], json_encode($postData), $options);
        // var_dump($result1);exit;
        return $result1;
    }

    function curlPost($url = '', $postData = '', $options = array())
    {
        if (is_array($postData)) {
            $postData = http_build_query($postData);
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
        if (!empty($options)) {
            curl_setopt_array($ch, $options);
        }
        //https请求 不验证证书和host
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $data = curl_exec($ch);

        curl_close($ch);
        return $data;
    }
    /**
     * 转换字符集编码
     * @param $data
     * @param $targetCharset
     * @return string
     */
    function characet($data, $targetCharset)
    {
        if (!empty($data)) {
            $fileType = $this->charset;
            if (strcasecmp($fileType, $targetCharset) != 0) {
                $data = mb_convert_encoding($data, $targetCharset, $fileType);
            }
        }
        return $data;
    }
    /**
     * 以appSecret为密钥,使用HmacSHA256算法对签名字符串生成消息摘要,对消息摘要使用BASE64算法生成签名(签名过程中的编码方式全为UTF-8)
     */
    function get_sign($postData, $url)
    {
        $sign_str = $this->get_sign_str($postData, $url); //签名字符串
        $app_secret = $this->app_secret;
        $sign = hash_hmac('sha256', $sign_str, $app_secret, true); //生成消息摘要
        $result = base64_encode($sign);
        return $result;
    }

    function get_sign_str($postData, $url)
    {
        $next = "\n";
        $str = "POST" . $next . $this->accept . $next . $this->content_type . $next . $this->get_date() . $next; //httpHeaders 
        $str .= "x-ca-key:" . $this->app_key . $next; //customHeaders 
        $str .= $url;
        return $str;
    }
    function get_date()
    {
        if (!$this->date)
            $this->date = date("Y-m-d H:i:s");
        return $this->date;
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

php 接入海康平台 的相关文章

随机推荐

  • Ubuntu下编辑权限只读文件的方法

    首先要注意的是只读文件一般都是系统文件 xff0c 或者软件配置文件 xff0c 修改时要尤其谨慎 本文提供两种方法 xff1a 方法一 首先安装一个插件 span class token function sudo span apt sp
  • Ros下Aruco模块的使用

    生成ARUCO ROS MARKER 链接 http chev me arucogen 首先启动ros roscore 打开相机节点 xff0c 在此提供usb相机与Realsense D435i的启动方法 xff1a roslaunch
  • 脚气、灰指甲治疗实验方案

    脚气 xff08 已临床实验 xff09 脚气 xff0c 又叫足廯 香港脚 糜烂性脚气 症状 xff1a 80 都是这种类型 常见于多汗人群 角质层被汗水浸软 xff0c 发白了以后 xff0c 走动不断摩擦表皮脱落 xff0c 露出鲜红
  • VS2015显示“正在从以下位置加载符号“的解决办法

    解决方法 xff1a VS 工具 选项 调试 符号 看到 MicroSoft符号服务器 xff0c 去掉方框中的 xff0c 确定即可 xff0c 之后就不会再调试时加载
  • 【面包】STM32学习笔记(二) --- USART 串口通信学习总结

    tip xff1a 如有错误 xff0c 希望指出 xff0c 非常感谢 xff01 目录 简介一 USART是什么 xff1f 二 问答通信方式1 USART和UART区别2 单工 半双工 全双工区别 三 代码实验1 说明2 代码初始化配
  • [论文阅读笔记] Reciprocal n-body Collision Avoidance(ORCA/RVO2)

    论文阅读 Reciprocal n body Collision Avoidance ORCA RVO2 文章目录 论文阅读 Reciprocal n body Collision Avoidance ORCA RVO2 论文地址Intro
  • 在VSCode中搭建C++编译环境

    在VSCode中搭建C 43 43 编译环境 VSCode当中搭建C 43 43 环境下载VSCode下载MinGW配置文件撰写测试小程序 VSCode当中搭建C 43 43 环境 vscode作为一款轻量级编程软件深受编程人员喜爱 xff
  • 【C++学习笔记】头文件详解

    个人整理学习用 xff0c 非教材 xff0c 有错误欢迎指正 头文件 究竟什么是头文件 xff1f 首先说明一个概念 xff0c 所谓的文件后缀并不是必须的 xff0c 在Linux下这种特点尤为明显 对于编译器来说 xff0c 无论是
  • 编程实现字符串连接函数strcat()

    按如下函数原型编程实现字符串连接函数strcat 的功能 void MyStrcat char dstStr char srcStr 输入提示信息 xff1a Input a string Input another string 输入字符
  • UDP通讯

    目录 利用DatagramSocket发送和接收UDP数据报 DatagramPacket构造方法说明 利用DatagramPacket和Datagramsocket简单实现服务器和客户端的通信 UDP协议通讯的用户状态跟踪 利用Datag
  • ubuntu 下C/C++文件编写

    1 Ubuntu下c cpp文件 1 1 cmake方式编译 cmake通常建立CmakeLists txt xff0c 通过cmake命令生成makefile文件编译工程 文件内容 xff1a span class token numbe
  • C语言基础入门:链表详解篇

    链表概述 链表是一种常见的重要的数据结构 它是动态地进行存储分配的一种结构 它可以根据需要开辟内存单元 链表有一个 头指针 变量 xff0c 以head表示 xff0c 它存放一个地址 该地址指向一个元素 链表中每一个元素称为 结点 xff
  • Linux c udp广播

    文章目录 1 对比2 代码2 1 服务端2 2 客户端 1 对比 服务端 xff1a 需要利用这个函数开发套接字的发广播权限 xff0c 并且需要客户端地址绑定为广播地址 span class token function setsocke
  • React—— HelloWorld

    React 学习笔记 Hello WorldJSX JavaScript XML 语法规则JavaScript 语法函数组件 类组件 amp 属性 props组合组件 生命周期函数 xff08 不全 xff09 amp 状态 state事件
  • Linux下makefile 编译项目

    文章目录 1 规划makefile编写2 makefile文件2 1 根目录下common mk2 2 config mk2 3 根目录makefile 2 4 其他目录下 1 规划makefile编写 a 根目录下放三个文件 xff1a
  • RPLIDAR激光雷达测试

    本文主要介绍PRLIDAR A2M8 R2激光雷达的的测试过程 关于该激光雷达的具体参数和描述 xff0c 可以直接去官网查询 本文的测试环境为Ubantu16 04 ROS xff08 kinetic xff09 关于Ubantu16 0
  • 【ROS机器人入门】1.1 ROS概念及环境配置

    文章目录 ROS设计目标系统要求配置步骤1 设置安装源2 设置ROS软件Key3 更新软件包4 安装完整版ROS Noetic软件5 配置ROS环境6 安装构建依赖7 1 安装rosdep 7 1与7 2任选其一 解决方法 7 2 安装ro
  • 纯C语言进行Get和Post请求(亲测)

    废话不多说 xff0c 直接上代码 span class token macro property span class token directive hash span span class token directive keywor
  • C++ 实现Get和Post请求(亲测)

    废话不多说 xff0c 直接上代码 span class token comment include lt stdlib h gt span span class token macro property span class token
  • php 接入海康平台

    php获取海康平台的监控流地址 先获取所有监控点 xff08 artemis api resource v1 cameras xff09 在根据监控点的cameraIndexCode请求 artemis api video v1 camer