微信公众号小程序授权登录

PHP 公众号授权登录

/**
 * PHP 公众号授权登录
 */
function myfun1()
{
    $appid = "wx************";
    $redirect_uri = urlencode('http://www.example.com/redirect.php'); // 回调地址
    $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
    header("Location: $url");
}

PHP 扫码关注公众号授权登录

/**
 * PHP 扫码关注公众号授权登录
 */
function myfun2()
{
    $appid = "wx************";
    $redirect_uri = urlencode('http://www.example.com/redirect.php'); // 回调地址
    $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
    header("Location: $url");
}

PHP 微信小程序授权登录

/**
 * PHP 微信小程序授权登录
 */
function getOpenid($code)
{
    $appid = "wx************";
    $secret = "************";
    $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appid . "&secret=" . $secret . "&js_code=" . $code . "&grant_type=authorization_code";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $output = curl_exec($ch);
    curl_close($ch);
    $jsoninfo = json_decode($output, true);
    $array = get_object_vars($jsoninfo);
    return $array;
}

PHP 微信授权登录返回401错误

/**
 * PHP 微信授权登录返回401错误
 */
function getAccessToken($code)
{
    $appid = "wx************";
    $secret = "************";
    $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
    $res = file_get_contents($url);
    $res = json_decode($res, true);
    return $res;
}

PHP 微信小程序授权登录记住小程序的登录状态

/**
 * PHP 微微信小程序授权登录记住小程序的登录状态
 */
function wxlogin($code)
{
    $appid = 'wx************';
    $secret = '************';
    $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";
    $res = file_get_contents($url);
    $res = json_decode($res, true);
    return $res;
}

猜你喜欢

转载自blog.csdn.net/withoutfear/article/details/128613623