php微信开发 大神看看有没有语法错误,能不能自动获取到最新access_token
伊谢尔伦
伊谢尔伦 2017-04-10 16:28:29
[PHP讨论组]
class Wechat_base_api{
        
        private $appid='wx5d4b7d2e8bfd5793';
        private $appsecret='4379ef7213c36eae5dc2ab91f9f6aced';
        //构造函数
        public function __construct($appid,$appsecret)
        {
            $this->appid = $appid;
            $this->appsecret = $appsecret;
        }
        
    //获取access_token(支持自动更新凭证)
        public function get_access_token()
        {
            $this->last_time = 1408851194;
            $access_token = "jIGpovtFGZDXCB_K2vqDPTA05zP7VWZaKIHXC_qZDqEiSGONWfCzQ45fI9aksl2L188yhtPpNB61iOBS4TTtgw";

            if(time() > ($this->last_time + 7200))
            {
                //GET请求的地址
                $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appid}&secret={$this->appsecret}";
                 $access_token_Arr =  $this->https_request($url);
                 $this->last_time = time();
                 return $access_token_Arr['access_token'];            
            }

            return $access_token;
        }
//https请求(支持GET和POST)
        protected function https_request($url,$data = null)
        {
            $ch = curl_init();
            curl_setopt($ch,CURLOPT_URL,$url);
            curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
            if(!empty($data))
            {
                curl_setopt($ch,CURLOPT_POST,1);//模拟POST
                curl_setopt($ch,CURLOPT_POSTFIELDS,$data);//POST内容
            }
            $outopt = curl_exec($ch);
            curl_close($ch);
            $outopt = json_decode($outopt,true);
            return $outopt;
        }
}
伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

全部回复(2)
伊谢尔伦

1.没有语法错误
2.这是谁家的id和secret...

天蓬老师

哥们 你们家的appid 跟appsecret 暴露了!!!
另外 access_token 是有次数限制的
记得缓存下来,不然请求次数过多,会出错的

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号