码迷,mamicode.com
首页 > Web开发 > 详细

php判断token有效期

时间:2019-08-17 17:52:13      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:his   exist   pre   签名   过期   sig   sha   nts   fopen   

         /*判断token文件是否存在*/
        if (file_exists("access_token.json"))
        {
            $result = json_decode(file_get_contents("access_token.json"),true);
             //file_get_content()读取文件可以使用
             //$fp = fopen($filename, ‘r‘)
             //$content = fread($fp, filesize($filename));
             // fclose($fp);

            $token = $result[‘token‘];
        }else{
            $result[‘token‘] = ‘‘;
            $result[‘expires_in‘] = ‘‘;
        }


        /*判断token是否过期*/
        if( $result[‘token‘] == ‘‘ || $result[‘expires_in‘] < time() ){
                $str = $this->AppID.$this->AppSecret.$this->SignKey;
                $Sign= MD5($str);/*签名*/
                $data = array(
                    ‘AppID‘ => $this->AppID,
                    ‘AppSecret‘ => $this->AppSecret,
                    ‘Sign‘ =>$Sign,
                );
                $token_json = $this->http_request($this->tokeurl,$data);
                $token = json_decode($token_json,true)[‘token‘];

                $reflesh = [];
                $reflesh[‘expires_in‘] = time()+3600;
                $reflesh[‘token‘] = $token;
                $fp = fopen("access_token.json", "w");
                fwrite($fp, json_encode($reflesh));
                fclose($fp);

        }
        return $this->token =$token;                                                                    

  

php判断token有效期

标签:his   exist   pre   签名   过期   sig   sha   nts   fopen   

原文地址:https://www.cnblogs.com/cnn2017/p/11369411.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!