当前位置 : 主页 > 网络编程 > PHP >

萤石云API简单操作

来源:互联网 收集:自由互联 发布时间:2021-06-28
操作萤石云API加载设备详情以及直播流 // | Technical support: http://evalor.cn//----------------------------------------------------------class hikVision{ private $appKey = '此处填入你的appKey'; private $appSecret = '此处
操作萤石云API加载设备详情以及直播流
 
// | Technical support: http://evalor.cn
//----------------------------------------------------------

class hikVision
{
    private $appKey = '此处填入你的appKey';
    private $appSecret = '此处填入你的appSecret';
    private $accessToken = null;
    private $device = [];
    private $video = null;

    /**
     * hikVision constructor.
     */
    public function __construct()
    {
        $EOL = preg_match("/cli/i", php_sapi_name()) ? PHP_EOL : '
'; $this->accessToken = $this->getToken(); if ($this->accessToken === false) die('AccessToken获取失败'); echo '已成功链接萤石中心' . $EOL; $this->device = $this->deviceList(); $this->video = $this->video(); if ($this->accessToken === false) die('设备列表获取失败'); echo '当前有 ' . count($this->device) . ' 台监控设备和 ' . count($this->video) . ' 个直播流:' . $EOL; foreach ($this->device as $value) { echo '
'; echo '' . $value->deviceSerial . ' ' . $value->deviceName . ' ' . $value->deviceType . '' . $EOL; echo $value->status == 1 ? '在线 ' : '离线 '; echo $this->version($value->deviceSerial) . $EOL; if ($EOL != PHP_EOL) { $picUrl = $this->capture($value->deviceSerial); if ($picUrl) { if ($this->video && array_key_exists($value->deviceSerial, $this->video) && $this->video[$value->deviceSerial]->status === 1) { // 有直播推流,初始化直播数据 $VideoInfo = $this->video[$value->deviceSerial]; echo " "; } else { echo "
"; } } else { echo "设备不在线,无法获取缩略图!
"; } } } } public function initPlayer() { echo ''; } public function getToken() { $data = ['appKey' => $this->appKey, 'appSecret' => $this->appSecret]; $retval = $this->sendPOST('https://open.ys7.com/api/lapp/token/get', $data); $retval = json_decode($retval); if ($retval === false) return false; if ($retval->code != 200) return false; return $retval->data->accessToken; } public function capture($deviceSerial) { $data = ['accessToken' => $this->accessToken, 'deviceSerial' => $deviceSerial, 'channelNo' => 1]; $retval = $this->sendPOST('https://open.ys7.com/api/lapp/device/capture', $data); $retval = json_decode($retval); if ($retval === false) return false; if ($retval->code != 200) return false; return $retval->data->picUrl; } public function video() { $data = ['accessToken' => $this->accessToken]; $retval = $this->sendPOST('https://open.ys7.com/api/lapp/live/video/list', $data); $retval = json_decode($retval); if ($retval === false) return false; if ($retval->code != 200) return false; // 转换对象结构 $videoList = []; foreach ($retval->data as $k => $video) { $deviceSerial = $video->deviceSerial; $videoList[$deviceSerial] = $video; } return $videoList; } public function version($deviceSerial) { $data = ['accessToken' => $this->accessToken, 'deviceSerial' => $deviceSerial]; $retval = $this->sendPOST('https://open.ys7.com/api/lapp/device/version/info', $data); $retval = json_decode($retval); if ($retval === false) return false; if ($retval->code != 200) return false; if ($retval->data->latestVersion === $retval->data->currentVersion) return '当前最新:' . $retval->data->currentVersion; return '可以升级:' . $retval->data->currentVersion . ' ==> ' . $retval->data->latestVersion; } public function deviceList() { $data = ['accessToken' => $this->accessToken]; $retval = $this->sendPOST('https://open.ys7.com/api/lapp/device/list', $data); $retval = json_decode($retval); if ($retval === false) return false; if ($retval->code != 200) return false; return $retval->data; } private function sendPOST($url, $data) { $postData = http_build_query($data); $opts = ['http' => [ 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $postData ]]; $context = stream_context_create($opts); $result = file_get_contents($url, false, $context); return $result; } } ?> 海康状态监控 initPlayer(); ?>
网友评论