php 微信授权原理

0311lc.com说:
  1. 微信授权有什么作用,微信授权我们需要使用微信开发的什么接口。微信授权,对于开发来说,他是与微信用户系统对接的唯一途径。为什么说是唯一途径,因为,只有通过微信授权,才能拿到用户在微信平台上的信息(头像,昵称,地址),例如:在进入微信商城的时候,弹出确认“xxx商城授权”。点击确定,就是该电商平台到微信去授权获取用户的信息。
  2. 微信授权,获取用户信息的接口(确保微信公众账号拥有授权作用域(scope参数)的权限的前提下引导用户去授权页面)
    • 静默授权  静默授权不需要用户确认,只需要用户访问某个网页,属于嵌套在普通网页里的授权形式,但是只能获取到用户的唯一标示openid,无法获取用户的个人信息
    • 网页授权 网页授权是一种通过用户确认,来获取用户的openid、个人信息、关注信息等的接口返回形式
  3. 在开发之前,我们要确保我们的服务号,在接口权限处,已经获得了网页授权权限

    此外,我们还需要有个备案通过的域名,比如”www.myname.com”,并确保你的域名可以访问到你的服务器,于是在公众号设置那里把域名配置好,把校验文件”MP_verify_lS1VtPAOta6l5jrQ.txt”放置到你的网站的根目录,于是提交成功,变成如下设置

    接下来,我们要去建立一个php文件”getinfo.php”,内容如下:
    <?php
    $appid = 'wxxxxa95xxx0xxxxx';
     
    #你的公众号appid
     
    $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=http%3A%2F%2Fwww.xingchuangpinzhi.com%2FgetinfoDetail.php&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
     
    #redirect_uri改为你的网页授权域名和刚刚跳转到的显示页面,比如我的是getinfoDetail.php
     
    header('location:'.$url);
    ?>
    

    建立一个php文件”getinfoDetail.php”,内容如下:

    <?php
    header("Access-Control-Allow-Origin:*"); #设置跨域
    header("Content-Type: application/json; charset=UTF-8"); 
    //获取存放在cookies里面的token
    $code = $_GET['code'];
    $state = $_GET['state'];
    //换成自己的接口信息
    $appid = 'wxxxxxxxxxxxxxx';#你的公众号appid
    $appsecret = '7xxfxxxxxxxxxxxxxxxxx';#你的公众号appsecret
    if (empty($code)) $this->error('授权失败');
    $token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
    $token = json_decode(file_get_contents($token_url));
    if (isset($token->errcode)) {
        echo '<h1>错误:</h1>'.$token->errcode;
        echo '<br/><h2>错误信息:</h2>'.$token->errmsg;
        exit;
    }
    $access_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$token->refresh_token;
    //转成对象
    $access_token = json_decode(file_get_contents($access_token_url));
    if (isset($access_token->errcode)) {
        echo '<h1>错误:</h1>'.$access_token->errcode;
        echo '<br/><h2>错误信息:</h2>'.$access_token->errmsg;
        exit;
    }
    $user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token->access_token.'&openid='.$access_token->openid.'&lang=zh_CN';
    //转成对象
    $user_info = json_decode(file_get_contents($user_info_url));
    if (isset($user_info->errcode)) {
        echo '<h1>错误:</h1>'.$user_info->errcode;
        echo '<br/><h2>错误信息:</h2>'.$user_info->errmsg;
        exit;
    }
     
    $rs =  json_decode(json_encode($user_info),true);//返回的json数组转换成array数组
    $openid= $rs["openid"];
    $nickname=$rs["nickname"];
    $sex=$rs["sex"];
    $language=$rs["language"];
    $city=$rs["city"];
    $province=$rs["province"];
    $country=$rs["country"];
    $headuri=$rs["headimgurl"];
    //打印用户信息
    echo $openid;
    echo $nickname;
    echo $sex;
    echo $language;
    echo $city;
    echo $province;
    echo $nickname;
    echo $headuri;
    echo $country;
    ?>
    

    完成后访问url:”http://www.我的域名.com/getinfo.php”,显示

    点击获取,打印出授权用户信息:


发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注