php获取海康平台的监控流地址 先获取所有监控点(/artemis/api/resource/v1/cameras)在根据监控点的cameraIndexCode请求/artemis/api/video/v1/cameras/previewURLs接口获取监控的视频流, 备注:h5播放的话获取视频流时取流协议protocol 要选择 hls,播放时要引入video.js
<?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";
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);
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);
}
public function get_previewURLs()
{
$cameraIndexCode = input('cameraIndexCode');
if(!$cameraIndexCode)
{
return "cameraIndexCode不能为空";
}
$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);
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);
if (!empty($options)) {
curl_setopt_array($ch, $options);
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
function characet($data, $targetCharset)
{
if (!empty($data)) {
$fileType = $this->charset;
if (strcasecmp($fileType, $targetCharset) != 0) {
$data = mb_convert_encoding($data, $targetCharset, $fileType);
}
}
return $data;
}
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;
$str .= "x-ca-key:" . $this->app_key . $next;
$str .= $url;
return $str;
}
function get_date()
{
if (!$this->date)
$this->date = date("Y-m-d H:i:s");
return $this->date;
}
}
|