获取微信access_token

query($_sql);
    if($row = $_result->fetch_assoc()){
        $accessToken = $row['accessToken'];
        $expired = $row['expired'];
        if($expired > time() + 600){
            return $result = array(
                'accessToken' => $accessToken,
                'expired' => $expired
            );
        }
    }
}

function getAccessTokenFromWX()
{
    GLOBAL $_mysqli;
    $appId = APPID;
    $appSecret = APPSECRET;
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appId&secret=$appSecret";
    $res = https_request($url);
    $res = json_decode($res);
    if($res->access_token){
        $id = 1;
        $accessToken = $res->access_token;
        $expired = $res->expires_in + time();
        $_sql = "INSERT INTO accessToken (id, accessToken, expired) VALUES ('{$id}', '{$accessToken}', '{$expired}')";
        $_sql = $_sql . "ON DUPLICATE KEY UPDATE accessToken=VALUES(accessToken), expired=VALUES(expired)";
        $_result = $_mysqli->query($_sql);
        if($_result){
            return $result = array(
                'accessToken' => $accessToken,
                'expired' => (string)$expired //因为数据库取出的值为字符串类型,这里为了返回的数据类型统一,也转换为字符串类型输出
            );
        }
    }
}
?>

你可能感兴趣的:(获取微信access_token)