PHP微信開発の取得access_token
1808 ワード
function curlGet($url){
$ch = curl_init();
$header = "Accept-Charset: utf-8";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$temp = curl_exec($ch);
return $temp;
}
function checkAccessToken($appid,$appsecret){
$condition = array('appid'=>$appid,'appsecret'=>$appsecret);
$access_token_set=M('AccessToken')->where($condition)->find();//
if($access_token_set){
// ,
if($access_token_set['AccessExpires']>time()){
// , access_token
return $access_token_set['access_token'];
}else{
// ,
$url_get='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret;
$json=json_decode(curlGet($url_get));
$access_token=$json->access_token;
$AccessExpires=time()+intval($json->expires_in);
$data['access_token']=$access_token;
$data['AccessExpires']=$AccessExpires;
$result = M('AccessToken')->where($condition)->save($data);//
if($result){
return $access_token;
}else{
return $access_token;
}
}
}else{
/* $appid,$appsecret ,
return 0;*/
}
}