码迷,mamicode.com
首页 > 微信 > 详细

微信开发三【获取access_token储存机制】

时间:2015-08-13 18:21:49      阅读:356      评论:0      收藏:0      [点我收藏+]

标签:

/**
     * 获取acess_token
     */
    public function Access_Token($appid, $appsecret)
    {
//         $appid = "";
//         $appsecret = "";

        //检验是否存在有效的access
        $access_token_info = Trend_Model_Access_Token::instance()->fetchByPK(1);
        $access_token_info = current($access_token_info);
        $now_time = strtotime(date(‘Y-m-d H:i:s‘));
        $ctime = strtotime($access_token_info[‘ctime‘]);
        $time_pass = ceil($now_time-$ctime);
        
        if ($time_pass >= $access_token_info[‘expires_in‘]) {
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $output = curl_exec($ch);
            curl_close($ch);
            $jsoninfo = json_decode($output, true);
            $access_token = $jsoninfo["access_token"];
            
            //更新到数据库
            $new_data = array(
                    ‘access_token‘ => $jsoninfo["access_token"],
                    ‘expires_in‘ => $jsoninfo["expires_in"],
                    ‘ctime‘ => date(‘Y-m-d H:i:s‘)
            );
            Trend_Model_Access_Token::instance()->updateForEntity($new_data, 1);
        } else {
            $access_token = $access_token_info[‘access_token‘];
        }
        
        return $access_token;
    }


微信开发三【获取access_token储存机制】

标签:

原文地址:http://my.oschina.net/hzfs/blog/491939

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