您好,欢迎访问三七文档
当前位置:首页 > 办公文档 > 求职简历 > 手把手教你php对接海康api
?phpheader('Content-type:text/html;Charset=utf-8');date_default_timezone_set('PRC');classHaikang{public$pre_url=访问IP;protected$app_key=app_key;protected$app_secret=app_secret;public$time;//时间戳public$content_type=application/json;//类型public$accept=*/*;//acceptpublic$person_list_url=/artemis/api/resource/v1/encodeDevice/get;//人员列表urlpublic$equipment_view_url=/artemis/api/video/v1/cameras/previewURLs;//获取监控点预览取流URLpublicfunction__construct($app_key='',$app_secret=''){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);}functiongetViewUrl($cameraIndexCode){//请求参数$postData['cameraIndexCode']=$cameraIndexCode;$postData['streamType']=0;$postData['protocol']='hls';$postData['transmode']=1;$sign=$this-get_sign($postData,$this-equipment_view_url);$options=array(CURLOPT_HTTPHEADER=array(Accept:.$this-accept,Content-Type:.$this-content_type,X-Ca-Key:.$this-app_key,X-Ca-Signature:.$sign,Date:.$this-time,X-Ca-Signature-Headers:.x-ca-key,));$result=$this-curlPost($this-pre_url.$this-equipment_view_url,json_encode($postData),$options);returnjson_decode($result,true);}/***获取人员列表*/functionget_person_list($response){//请求参数$postData['pageNo']=isset($response['pageNo'])?intval($response['pageNo']):1;$postData['pageSize']=isset($response['pageSize'])?intval($response['pageSize']):1000;$sign=$this-get_sign($postData,$this-person_list_url);$options=array(CURLOPT_HTTPHEADER=array(Accept:.$this-accept,Content-Type:.$this-content_type,X-Ca-Key:.$this-app_key,X-Ca-Signature:.$sign,Date:.$this-time,X-Ca-Signature-Headers:.x-ca-key,));$result=$this-curlPost($this-pre_url.$this-person_list_url,json_encode($postData),$options);returnjson_decode($result,true);}/***以appSecret为密钥,使用HmacSHA256算法对签名字符串生成消息摘要,对消息摘要使用BASE64算法生成签名(签名过程中的编码方式全为UTF-8)*/functionget_sign($postData,$url){$sign_str=$this-get_sign_str($postData,$url);//签名字符串$priKey=$this-app_secret;$sign=hash_hmac('sha256',$sign_str,$priKey,true);//生成消息摘要$result=base64_encode($sign);return$result;}functionget_sign_str($postData,$url){$next=\n;$str=POST.$next.$this-accept.$next.$this-content_type.$next.$this-time.$next;$str.=x-ca-key:.$this-app_key.$next;$str.=$url;return$str;}publicfunctiongetSignContent($params){ksort($params);$stringToBeSigned=;$i=0;$len=count($params);foreach($paramsas$k=$v){if(false===$this-checkEmpty($v)&&@!=substr($v,0,1)){//转换成目标字符集$v=$this-characet($v,$this-charset);if($i==0){$stringToBeSigned.=?$k.=.$v;}else{$stringToBeSigned.=&.$k.=.$v;}$i++;}}unset($k,$v);return$stringToBeSigned;}functionget_message($postData){$str=str_replace(array('{','}',''),'',json_encode($postData));returnbase64_encode(md5($str));}/***校验$value是否非空*ifnotset,returntrue;*ifisnull,returntrue;**/protectedfunctioncheckEmpty($value){if(!isset($value))returntrue;if($value===null)returntrue;if(trim($value)===)returntrue;returnfalse;}/***转换字符集编码*@param$data*@param$targetCharset*@returnstring*/functioncharacet($data,$targetCharset){if(!empty($data)){$fileType=$this-charset;if(strcasecmp($fileType,$targetCharset)!=0){$data=mb_convert_encoding($data,$targetCharset,$fileType);}}return$data;}publicfunctioncurlPost($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);//设置cURL允许执行的最长秒数if(!empty($options)){curl_setopt_array($ch,$options);}//https请求不验证证书和hostcurl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);$data=curl_exec($ch);curl_close($ch);return$data;}}
本文标题:手把手教你php对接海康api
链接地址:https://www.777doc.com/doc-6408115 .html