最近有一个项目需要对接海康监控,这里我写了一个demo,方便我们来调用海康监控的相关接口
海康监控接口文档:https://open.hikvision.com/do...
海康监控demo:
<?php
namespace common\helpers;
use common\models\Config;
use Yii;
use yii\helpers\ArrayHelper;
use common\helpers\Universal;
use yii\httpclient\Client;
/**
* 海康开放平台接口
*
* @author wangjian
* @since 0.1
*/
class Artemis
{
//接口域名
public $url = '';
/**
* @var string app_key
*/
public $app_key = '';
/**
* @var string app_secret
*/
public $app_secret = '';
//毫秒级时间戳
private $msec_time;
public $content_type = "application/json";//类型
public $accept = "*/*" ;//accept
public $method = 'POST';
//接口地址
private $api_url;
//传递参数
private $data;
/**
* @param string $api_url
* @param string $method
* @param array $data
*/
public function __construct($api_url = '', $method = '', $data = [])
{
if ($api_url) {
$this->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();
如上既可以获取到监控点资源,其他相关接口调用方式参照如上即可
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。