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

/**
     * 获取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储存机制】

上一篇:微信支付h5客户端开发步骤


下一篇:为Jfinal-weixin SDK添加微信连WiFi成功通知事件