zoukankan      html  css  js  c++  java
  • PHP获取access_token

        /**
         * 获取accesstoken
         *
         * @param  int  $id
         * @return 	hinkResponse
         */
        public function AccessToken($appid)
        {
            //获取url
            $file = $this->accesstoken_url;
    
            if(!file_exists($file)){
                $token = $this->get_accesstoken($appid);
            }else{
    
                $fileInfo = explode(",",file_get_contents($file));
    
                if(!empty(trim($fileInfo[0])) && $fileInfo[1] >= time()){
                    $token = $fileInfo[0];
                }else{
                    $token = $this->get_accesstoken($appid);
                }
    
            }
    
    
            return $token;
        }
    
        /**
         * 获取token
         */
        private function get_accesstoken($appid)
        {
            $data = [
                "grant_type"=>"client_credential",
                "appid"=>$appid,
                "secret"=>$this->AppSecret
            ];
    
            $url = $this->url_token;
    
            $tokeninfo = $this->jsonDecode(https_request($url,$data),"access_token");
    
            if($tokeninfo){
                $text = $tokeninfo.",".(time()+7000);
    
                //写入文件中
                $file = $this->accesstoken_url;
        
                if(!file_exists($file)){
                    fopen($file,"wb");
                }
                //把值存入文件中
                $myfile = fopen($file,"w");
    
                fwrite($myfile, $text);//写入文件
    
                fclose($myfile);//关闭文件
            }
    
            return  $tokeninfo;
        }
    
        /**
         * json数据处理
         */
        private function jsonDecode($data,$key)
        {
            $new_data = json_decode($data,true);
    
            if(array_key_exists($key,$new_data)){
                return $new_data[$key];
            }
            return $data;
        }
  • 相关阅读:
    nodejs + mongodb
    实习踩坑
    jQuery获取点击对象的父级
    python正则表达式
    python文件基础IO,OS
    python模块
    python时间和日期
    python number
    python循环
    Vue2.0 【第一季】第6节 v-model指令
  • 原文地址:https://www.cnblogs.com/corvus/p/13029155.html
Copyright © 2011-2022 走看看