Token.php 4.5 KB
<?php
namespace isc;
use fast\Http;
use think\Db;
class Token{
    protected $url = 'https://220.163.129.62:1443/artemis';
    // public function _initialize(){
    //     $this->checkToken();   
    // }
    public function getToken(){
        $appkey = '27643551';
        $appsecret = 'FnQ0YQ3JBKbVfqYVonW7';
        $Accept = '*/*';
        $ContentType = 'application/json';
        $Date = date('Y-m-d H:i:s',time());
        $url = 'https://220.163.129.62:1443/artemis/api/v1/oauth/token';
        $data = [];
        //$Date = 'Fri Feb 01 10:22:21 CST 2019';
        $XCaKey = '27643551';
        $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;
    }


}