最近有一个我的项目须要对接海康监控,这里我写了一个 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();
如上既能够获取到监控点资源,其余相干接口调用形式参照如上即可