微信公众平台开发-网页授权

代码参考自慕课网视频https://www.imooc.com/learn/619

代码GitHub地址:https://github.com/xiaoming000/wechat.git

 

网页授权分为静默授权和手动同意授权,手动同意授权需要用户同意,可以获得用户的更多信息

官方文档地址:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842

 

静默模式

code.php

<?php
require "../public.php";

class GetCode{



    // 1、引导用户进入授权页面同意授权,获取code
    public function get_code(){
        // https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI
        // &response_type=code&scope=SCOPE&state=STATE#wechat_redirect 
        // 若提示“该链接无法访问”,请检查参数是否填写错误,是否拥有scope参数对应的授权作用域权限
        $appid = "wx70b222a95832e8b1";
        $redirect_uri = urlencode("http://wechat.dmfly.xin/power/userinfo.php");
        $scope = "snsapi_base";
        $fun = new PublicFunction();
        $state = $fun->get_rand_code(16);
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=".$scope."&state=".$state."#wechat_redirect";

        header('location:'.$url);
    }


    // 3、如果需要,开发者可以刷新网页授权access_token,避免过期

    // 4、通过网页授权access_token和openid获取用户基本信息(支持UnionID机制)

} // class end

// 运行
$code = new GetCode();
$code -> get_code();

 

userinfo.php

<?php

require "../public.php";

// 2、通过code换取网页授权access_token(与基础支持中的access_token不同)
function get_user_info(){
    // 获取code后,请求以下链接获取access_token:  https://api.weixin.qq.com/sns/oauth2/access_token?appid=
    // APPID&secret=SECRET&code=CODE&grant_type=authorization_code
    $appid = "wx70b222a95832e8b1";
    $secret = "afef21cf29c9e17c60c58ea37a210aa2";
    $code = $_GET['code'];
    $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$secret."&code=".$code."&grant_type=authorization_code";
    $fun = new PublicFunction();
    $res = $fun -> http_curl($url);
    var_dump($res);
}


// 运行
get_user_info();

 

手动同意授权

code_detail.php

<?php

// 通过access_token获取用户信息这里主要就是更改scope为snsapi_userinfo
class GetCode{

    // 获取随机数state, 最多128个字节
    public function get_state($num){
        $resource = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N','O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'S', 'Y', 'Z',
            '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'
            );
        $len = count($resource);
        $result = '';
        for ($i=0; $i < $num; $i++) { 
            $result.= $resource[rand(0, $len-1)];
        }
        return $result;
    }

    // 1、引导用户进入授权页面同意授权,获取code
    public function get_code(){
        // https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI
        // &response_type=code&scope=SCOPE&state=STATE#wechat_redirect 
        // 若提示“该链接无法访问”,请检查参数是否填写错误,是否拥有scope参数对应的授权作用域权限
        $appid = "wx70b222a95832e8b1";
        $redirect_uri = urlencode("http://wechat.dmfly.xin/power/userinfo_detail.php");
        $scope = "snsapi_userinfo";
        $state = $this->get_state(16);
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=".$scope."&state=".$state."#wechat_redirect";

        header('location:'.$url);
    }


    // 3、如果需要,开发者可以刷新网页授权access_token,避免过期

    // 4、通过网页授权access_token和openid获取用户基本信息(支持UnionID机制)

} // class end

// 运行
$code = new GetCode();
$code -> get_code();

 

userinfo_detail.php

<?php

require "../public.php";

// 2、通过code换取网页授权access_token(与基础支持中的access_token不同)
function get_user_info(){
    // 获取code后,请求以下链接获取access_token:  https://api.weixin.qq.com/sns/oauth2/access_token?appid=
    // APPID&secret=SECRET&code=CODE&grant_type=authorization_code
    $appid = "wx70b222a95832e8b1";
    $secret = "afef21cf29c9e17c60c58ea37a210aa2";
    $code = $_GET['code'];
    $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$secret."&code=".$code."&grant_type=authorization_code";
    $fun = new PublicFunction();
    $res = $fun -> http_curl($url);
    $access_token = $res['access_token'];
    $openid = $res['openid'];

    // 获取用户详细信息
    // https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
    $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
    $res = $fun -> http_curl($url);
    var_dump($res);
}


// 运行
get_user_info();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值