首页 > 日志 > php获取微信公众号access_token并存储到数据库

php获取微信公众号access_token并存储到数据库

function getJson($url){
$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);
return json_decode($output, true);

}

function accessToken() {

$result = mysql_query("select * from qckj_wx order by id desc");
$tongji = mysql_num_rows($result);
$myrow = mysql_fetch_assoc($result);
if($tongji == 0 or $myrow['gettime'] > time()) {
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=填写获取的&secret=填写获取的";
    $res = getJson($url);
    $access_token = $res['access_token'];
    $gettime = time() + 7000;
    if ($tongji != 0){$exec="update qckj_wx set access_token='".$access_token."',gettime='".$gettime."' where id = 1";}
    else {$exec="insert into qckj_wx (access_token,gettime) values ('".$access_token."','".$gettime."')";}
    mysql_query($exec);
}
else {
    $access_token = $myrow['access_token'];
}
return $access_token;

}

上一篇: 晚安心语:你可以爱一个人到尘埃里,但没有人爱尘埃里的你

下一篇: php 微信公众号发送模板消息

联盟广告

最新文章