php判断token有效期

时间:2019-08-17 18:00:01   收藏:0   阅读:424
         /*判断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;                                                                    

  

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

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!