Get Access_token storage using

Source: Internet
Author: User

/*

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.