海康api接口鉴权示例

一、设备接入

1
2
3
4
1. 打开设备,打开网络设置,连接wifi网络。
2. 开启ISUP协议,配置相关信息。
3. 打开国标,配置国标协议信息。
4. 保存设置并退出,设备将按照新设置进行工作。

二、API鉴权

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#接口加密鉴权
public static function makeSignature(string $str) {
$sk = "you secret";
$sign = hash_hmac('sha256', $str, $sk, true);
return base64_encode($sign);
}

public static function makeSignStr(string $url, array $header = []) {
$str = "POST\n"
. $header['Accept'] . "\n";
!empty($header['Content-MD5']) && $str .= $header['Content-MD5'] . "\n";
$str .= $header['Content-Type'] . "\n"
. "x-ca-key:{$header['X-Ca-Key']}\n"
. "x-ca-timestamp:{$header['X-Ca-Timestamp']}\n";
$str .= $url;
return $str;
}

public static function signature(string $url, array $header = []) {
$str = self::makeSignStr($url, $header);
return self::makeSignature($str);
}

public static function getHeaders(array $headers){
$data = [];
foreach($headers as $k => $v) {
$data[] = $k.":".$v;
}
return $data;
}

二、API请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public function requestApi()
{
$appKey = "you appkey";
$headers = [
"Accept" => "*/*",
"Content-Type" => "application/json",
"X-Ca-Key" => $appKey,
"X-Ca-Signature" => "",
"X-Ca-Signature-Headers" => "x-ca-key,x-ca-timestamp",
"X-Ca-Timestamp" => time()."000"
];
$url = '/artemis/api/brecorder/v3/deviceApiService/device/control';
$headers['X-Ca-Signature'] = self::signature($url, $headers);
$headers = self::getHeaders($headers);
$data = [
"batchDeviceIds" => ['设备编号'],
"command" => 2
];
$apiUrl = "https://192.167.1.10:4443/artemis/api/brecorder/v3/deviceApiService/device/control";
list($body, $header) = HttpCurl::request($apiUrl, "post", json_encode($data), $headers);

return json(json_decode($body,true));
}

返回最终结果如下:

1
2
3
4
5
{
"code": "0",
"msg": "SUCCESS",
"data": null
}
-------------本文结束感谢您的阅读-------------
0%