Token.php
4.5 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
namespace isc;
use fast\Http;
use think\Db;
class Token{
protected $url = 'https://139.9.65.252:1443/artemis';
// public function _initialize(){
// $this->checkToken();
// }
public function getToken(){
$appkey = '29492226';
$appsecret = '82TBBS3plLUTx1XftSh2';
$Accept = '*/*';
$ContentType = 'application/json';
$Date = date('Y-m-d H:i:s',time());
$url = 'https://139.9.65.252:1443/artemis/api/v1/oauth/token';
$data = [];
//$Date = 'Fri Feb 01 10:22:21 CST 2019';
$XCaKey = '29492226';
$XCaSignature = '';
$XCaSignature .= 'POST'."\n";
$XCaSignature .= $Accept."\n";
$XCaSignature .= $ContentType."\n";
$XCaSignature .= $Date."\n";
$XCaSignature .= 'x-ca-key:'.$XCaKey."\n";
$XCaSignature .= '/artemis/api/v1/oauth/token';
$sign = hash_hmac('sha256',$XCaSignature,$appsecret,true);
$XCaSignature = base64_encode($sign);
$XCaSignatureHeaders = 'x-ca-key';
$headerArray =array("Content-Type: $ContentType","Accept:$Accept","Date:$Date","X-Ca-key:$XCaKey","X-Ca-Signature:$XCaSignature","X-Ca-Signature-Headers:$XCaSignatureHeaders");
$user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$output = curl_exec($curl);
curl_close($curl);
$res = json_decode($output,true);
if($res['code'] == '0'){
$time = time()+$res['data']['expires_in']-60;
Db::name('reservoir_hkws_token')->where('1=1')->update(['access_token'=>$res['data']['access_token'],'expire_time'=>$time]);
}
else{
Db::name('reservoir_hkws_log')->insert(['message_log'=>json_encode($res),'createtime'=>time()]);
echo '获取token出错';
}
}
public function token(){
$info = Db::name('reservoir_hkws_token')->find();
if($info['expire_time'] && time() >= $info['expire_time'] || !$info['expire_time']){
$this->getToken();
$info = Db::name('reservoir_hkws_token')->find();
return $info['access_token'];
}
else{
return $info['access_token'];
}
}
public static function getMillisecond(){
list($msec, $sec) = explode(' ', microtime());
$msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
return $msectimes = substr($msectime,0,13);
}
//请求海康威视API使用
public function posturl($url,$data,$headerArray = array(),$cookie = '',$type = ''){
$token = $this->token();
$data['pageNo'] = 1;
$data['pageSize'] = 1000;
date_default_timezone_set('PRC');
if(!$headerArray){
$headerArray =array("Content-Type: application/json","access_token:$token");
}
//$user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $this->url.$url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
if($type === 0 && $cookie){
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie);
}
elseif($type === 1 && $cookie){
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie);
}
//curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);
//curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
}