海康监控接口调用

最近有一个项目需要对接海康监控,这里我写了一个demo,方便我们来调用海康监控的相关接口

海康监控接口文档:https://open.hikvision.com/do...

海康监控demo:

api_url = $api_url;
        }
        if ($method) {
            $this->method = $method;
        }

        $this->data = $data;

        $this->msec_time = $this->msectime();
        $this->charset = 'utf-8';

    }

    /**
     * 调用海康接口
     */
    public function apiInterface()
    {
        $sign = $this->get_sign($this->method,$this->api_url);
        $headers = [
            CURLOPT_HTTPHEADER => [
            "Accept:".$this->accept,
            "Content-Type:".$this->content_type,
            "x-Ca-Key:".$this->app_key,
            "X-Ca-Signature:".$sign,
            "X-Ca-Timestamp:".$this->msec_time,
            "X-Ca-Signature-Headers:"."x-ca-key,x-ca-timestamp",
            ]
            
        ];
        $result  = $this->curl($headers);
        return json_decode($result,true);

    }

    /**
     * 海康接口调用
     * @param string $url
     * @param string $postData
     * @param array $headers
     * @return bool|string
     */
    public function curl($headers = array())
    {
        $data = json_encode($this->data);
        if (is_array($data)) {
            $data = http_build_query($data);
        }
        $ch = curl_init();
        $url = $this->url . $this->api_url;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if ($this->method = 'POST') {
            curl_setopt($ch, CURLOPT_POST, 1);
        } else {
            curl_setopt($ch, CURLOPT_POST, 0);
        }
        if ($data) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数

        if (!empty($headers)) {
            curl_setopt_array($ch, $headers);
        }

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

    /**
     * 签名
     * 以appSecret为密钥,使用HmacSHA256算法对签名字符串生成消息摘要,对消息摘要使用BASE64算法生成签名(签名过程中的编码方式全为UTF-8)
     */
    function get_sign($method,$url){
        $sign_str = $this->get_sign_str($method,$url); //签名字符串
        $sign = hash_hmac('sha256', $sign_str, $this->app_secret,true); //生成消息摘要
        $result = base64_encode($sign);
        return $result;
    }

    /**
     * 获取签名字符串
     *
     * @param $postData
     * @param $url
     * @return string
     */
    function get_sign_str($method,$url){
        $next = "\n";
        $str = $method . $next;
        $str .= $this->accept . $next;
        $str .= $this->content_type . $next;
        $str .= "x-ca-key:" . $this->app_key.$next;
        $str .= "x-ca-timestamp:" . $this->msec_time.$next;
        $str .= $url;
        return $str;
    }
    
    /**
     * 获取毫秒级时间戳
     */
    public function msectime() {
        list($msec, $sec) = explode(' ', microtime());
        $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        return $msectime;
    }
}

需要将demo中url,app_key,app_secret改成您自己的即可

使用海康监控demo简单示例
分页获取监控点资源

$data = [
     'pageNo' => 1,//目标页码,范围 (0 , ~ )
     'pageSize' => 1000,//每页记录数,范围 (0 , 1000)
 ];
 $apiUrl = '/artemis/api/resource/v1/cameras';
 $artemis = new Artemis($apiUrl, 'POST', $data);
 $data=$artemis->apiInterface();

如上既可以获取到监控点资源,其他相关接口调用方式参照如上即可

你可能感兴趣的:(php监控)