获得accesstoken的方法

public function getAccessToken()
{
$accesstokenTable= M('weixin_accesstoken')->find();
if($accesstokenTable==false||$accesstokenTable['time']+7000<time())
{
$appid=M('weixin_appid')->find();
$wxurl='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid['appid'].'&secret='.$appid['secret'];
$result=$this->https_request($wxurl,null);
$arr=json_decode($result,true);
$accesstoken=$arr['access_token'];
$time=time();
$data=array();
$data['appid']=$appid['appid'];
$data['accesstoken']=$accesstoken;
$data['time']=$time;
M('weixin_accesstoken')->add($data);
}else{
$accesstoken=$accesstokenTable['accesstoken'];
}
return $accesstoken;
}
public function https_request($url,$data = null){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}

你可能感兴趣的:(Access)