微信接口开发之高级篇系列【网页受权详细说明【提供测试帐号使用】】

 本篇文章你将学到:在本身作的微信网站里,利用oauth2.0网页受权接口获取用户的信息(openid,姓名,性别,地区,头像等)。如大转盘等游戏记录哪一个微信用户得到什么奖品、H5等小游戏须要把分数与对应用户捆绑在一块儿等网页应用。html

微信公众平台oauth2.0网页受权能干什么

它是在本身作的网站中不用用户登陆来获取微信用户相关信息的,进而实现相关业务。json

说明与注意

一、网页受权分为两种,api

     一种为只获取openid  (基本受权 snsapi_base)数组

     一种为获取用户所有信息 (高级受权 snsapi_userinfo)。浏览器

二、你的公众号必须为认证的订阅号或者认证的服务号。不然没有此接口权限。微信

三、你要配置好回调域名:即用户点击网址获取用户信息后打开哪一个域名。app

四、若有下图错误请检查是否配置好回调域名或者公众号是否定证(我以前一直测试提示以下图出错,仔细查找错误才发现没配置回调域名)微信公众平台

怎样配置回调域名

一、进入https://mp.weixin.qq.com,点击最下面的”接口权限“菜单(以下图)
框架

1-一、若是是测试帐号的话,以下图curl

  (1)打开浏览器,这里以IE为例,输入:http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

     (2)

     (3)用手机登陆你的微信,使用微信中的“扫一扫”功能,扫描上面网页中的二维码。在手机上会出现如下界面:

        

     (4)网页受权获取用户基本信息

二、找到‘网页受权用户基本信息’,以下图


三、点击修改,填写域名。如:个人回调网址为http://wechatu.xd107.com/home/WeiXin/index 则填写wechatu.xd107.com。配置回调域名完成。

无论获取openid仍是用户全部信息都须要首先配置回调域名

 四、实际代码(ThinkPhp框架)

class WeiXinController extends Controller
{

    /**
     * 这里采用高级受权模式,为了获取用户信息
     * 这个页面是用户进来就可以刚问的页面,也就是首次进来的页面
     * 首先访问的地址 ;http://wechatu.xd107.com/home/WeiXin/index
     */
    public function index()
    {
        $appid = 'wx94c43716d8a91f3f';
        /*基本受权 方法跳转地址*/
        $redirect_uri = urlencode('http://wechatu.xd107.com/home/WeiXin/getUserInfo');

        /*基本受权 snsapi_base*/
        //$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_base&state=1234#wechat_redirect";

        /*高级受权 snsapi_userinfo*/
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $appid . "&redirect_uri=" . $redirect_uri . "&response_type=code&scope=snsapi_userinfo&state=1234#wechat_redirect";
        //跳转
        header('location:' . $url);
    }

    /*拉取用户信息*/
    public function getUserInfo()
    {
        $appid = 'wx94c43716d8a91f3f';
        $appsecret = 'd4624c36b6795d1d99dcf0547af5443d';
        /*回调的时候自带的这个参数*/
        $code = $_GET['code'];

        /*基本受权 snsapi_base*/
        //$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_base&state=1234#wechat_redirect";
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $appid . "&secret=" . $appsecret . "&code=" . $code . "&grant_type=authorization_code";

        $result = json_decode(curlPost($url,$parm = null),true);

        /*取出数组中的access_token这个值*/
        $access_token = $result['access_token'];
        $openid = $result['openid'];
        $URL2 = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $access_token . "&openid=" . $openid . "&lang=zh_CN";
        $responseInfo = json_decode(curlPost($URL2,$parameter = null),true);
        $_SESSION['headimgurl'] = $responseInfo['headimgurl'];
        var_dump($responseInfo);
        die;
        $this->headimgurl = $responseInfo['headimgurl'];
        $this->userInfo = $responseInfo;
        $this->display();
    }
}

 五、流程图(百度脑图)

说明:开始详解:(1)网页受权分为两种,(2)微信公众帐号和用户的微信联系字段为【openid】

 

六、具体步骤:

 

 

相关文章
相关标签/搜索