/*
Table structure
CREATE TABLE ' Wx_menuset ' (
' Menusetid ' int (one) not NULL auto_increment,
' AppID ' varchar DEFAULT NULL,
' Appsecret ' varchar DEFAULT NULL,
' Accesstoken ' varchar (+) DEFAULT NULL,
' Lasttime ' int (ten) is DEFAULT NULL,
PRIMARY KEY (' Menusetid ')
) Engine=myisam auto_increment=1 DEFAULT Charset=utf8;
Menusetid primary Key ID
AppID platform AppID
Appsecret Platform Appsecret
Accesstoken Access_token string values obtained through AppID and Appsecret
Lasttime time to last update Access_token
# First Use, you need to manually add a record in the table to fill in the Menusetid and AppID and Appsecret fields (the Accesstoken field is left blank)
*/
functionAccesstoken () {
$sql= ' SELECT menusetid,appid,appsecret,accesstoken,lasttime from Wx_menuset ORDER by lasttime DESC LIMIT 1 ';
$res=mysql_query($sql);
$row=Mysql_fetch_assoc($res);
if(isset($row[' Lasttime '])) {
if($row[' Lasttime ']+7000> Time() &&$row[' Accesstoken ']!= ') {
return$row[' Accesstoken '];
}Else{
$appid=$row[' AppID '];
$appsecret=$row[' Appsecret '];
$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"];
$sql=sprintf(' UPDATE wx_menuset SET accesstoken = \ '%s\ ', lasttime = \ '%d\ ' WHERE Menusetid =%d ',$access _token, Time(),$row[' Menusetid ']);
$res=mysql_query($sql);
if($res){
return$access _token;
}Else{
returnfalse;
}
}
}Else{
returnfalse;
}
}
Get Access_token storage using