获取用户微信openid用户无感知情况下 传参为 appid appsecret 当前网址 session_name名称

<?php
//获取微信的openid
function get_wx_str($appid,$appsecret,$link,$session_name='')
{
    if(empty($link))
    {
        $link = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    }
    parse_str($_SERVER['QUERY_STRING'], $query);
    if(!isset($query['code']))
    {
        //未授权
        $redirect = urlencode($link);
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?app>;
        redirect($url);
    }else
    {
        //已授权
        $code = $query['code'];
        $jsoninfo = get_wx_openid($appid,$appsecret,$code);
        $user_str = $jsoninfo['openid'];
        if($session_name!='')
        {
            $_SESSION[$session_name] = $user_str;
        }else
        {
            $_SESSION['user_str'] = $user_str;
        }
    }
}

//通过code换取网页授权access_token
function get_wx_openid($appid,$appsecret,$code)
{
    $url = "https://api.weixin.qq.com/sns/oauth2/access_token?app>;
    $output = https_request($url);
    $jsoninfo = json_decode($output, true);
    return $jsoninfo;
}
?>

 

相关文章:

  • 2022-12-23
  • 2022-02-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-05
  • 2021-12-23
  • 2021-06-14
猜你喜欢
  • 2022-01-18
  • 2022-12-23
  • 2022-12-23
  • 2021-07-20
  • 2021-07-06
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案