1
您的位置: 新微云首页  >  资讯列表  >  正文

海康威视PHP接口api对接代码php对接海康卫视对接海康人脸识别api接口之PHPphp调用海康,手把手教你php对接海康api

编辑:小峰   2024-03-11 15:17:17.84.13.64

最近对接海康威视的接口,在网上找到的相关PHP的对接代码。 亲测可用。大家可以参考对接。

 文档代码复制的来源

https://blog.csdn.net/weixin_45724083/article/details/122976505

<?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;
    }
} 



文章内容如有涉及侵权,请联系作者进行删除。

TOP

在线客服

联系我们