private function renewToken()
 {
     try {
         $url = sprintf(self::API_URL_PREFIX . self::TOKEN, $this->appid, $this->secret);
         $curl_response = $this->curl_request($url);
         if ($curl_response['code'] != 200) {
             throw new Exception($curl_response['error']);
         }
         $result = json_decode($curl_response['data'], true);
         if (isset($result['errcode'])) {
             throw new Exception($result['errmsg']);
         } else {
             if (isset($result['access_token'])) {
                 $data = array('app_token' => $result['access_token'], 'app_token_expire' => 6000 + intval(time()));
                 set_access_token($data);
                 $this->access_token = $data['app_token'];
                 $this->expires_in = $data['app_token_expire'];
             }
         }
     } catch (Exception $ex) {
         # log the exception
         log_error('WeChat: renewToken', json_encode($curl_response), $ex->getMessage());
         return false;
     }
     return $data['app_token'];
 }
Example #2
0
            }
            responseJSON($errmsg);
        }
        unset($_wechat);
        $mysql->beginTransaction();
        foreach ($SYSCONFIG['APP_CONFIG'] as $config) {
            if (isset($_POST[$config])) {
                $query = "UPDATE app_configs SET value = ?\tWHERE name = ? ";
                $bindArr = array($_POST[$config], $config);
                if (!$mysql->execute($query, $bindArr)) {
                    throw new Exception($mysql->error);
                }
            }
        }
        $mysql->commit();
        // update the current session & configs
        $data = array('app_token' => $result['access_token'], 'app_token_expire' => time() + intval($result['expires_in']));
        set_access_token($data);
        $SYSCONFIG['WECHAT']['app_name'] = $app_name;
        $SYSCONFIG['WECHAT']['thumb'] = $app_icon;
        $SYSCONFIG['WECHAT']['appid'] = $appid;
        $SYSCONFIG['WECHAT']['app_secret'] = $app_secret;
    } catch (Exception $ex) {
        $mysql->rollBack();
        # log the exception
        log_error('app_settings: save', json_encode($_POST), $ex->getMessage());
        $returnArr['code'] = 500;
        $returnArr['err'] = $ex->getMessage();
    }
    responseJSON($returnArr);
}