两种微信网页授权方式snsapi_userinfo和snsapi_base代码

新工作新环境,还在熟悉新项目中。。。。。。。
新工作的业务场景是微信公总号商城,就看了下微信公总号开发流程,从最基础开始,网页授权流程:

1、引导用户进入授权页面同意授权,获取code
2、通过code换取网页授权access_token(与基础支持中的access_token不同)
3、如果需要,开发者可以刷新网页授权access_token,避免过期
4、通过网页授权access_token和openid获取用户基本信息(支持UnionID机制)

应用场景有两种
1、snsapi_base 静默授权,用户无感知,但只能获取用户的openid,而且用户必须关注微信公众号才能正常执行
2、snsapi_userinfo 弹出授权页面,需要用户手动点击授权,能获的openid和用户的基本信息,切用户无需关注公众号就能执行

snsapi_base 代码:

//index.php 
<?php
    //snnapi_base模式
    $appid='你的appid';
    $redirect_uri = urlencode ( 'http://你的域名/getUserInfo.php' );

    $url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_base&state=1#wechat_redirect";

    header("Location:".$url);
?>


<?php
    $appid = "你的appid";  
    $secret = "你的secret";  
    $code = $_GET["code"];
    //第一步:取全局access_token
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
    $token = getJson($url);

    //第二步:取得openid
    $oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
    $oauth2 = getJson($oauth2Url);

    //第三步:根据全局access_token和openid查询用户信息  
    $access_token = $token["access_token"];  
    $openid = $oauth2['openid'];  
    $get_user_info_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";
    $userinfo = getJson($get_user_info_url);

//打印用户信息
print_r($userinfo);
function getJson($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    return json_decode($output, true);
}

snsapi_userinfo代码

//index.php 
<?php
    //snnapi_base模式
    $appid='你的appid';
    $redirect_uri = urlencode ( 'http://你的域名/getUserInfo.php' );

    $url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";

    header("Location:".$url);
?>

getUserInfo.php

<?php

    $appid = "你的openid";  
    $secret = "你的secret";  
    $code = $_GET["code"];
    //snsapi_userinfo实例
    //第一步:取得openid
    $oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
    $oauth2 = getJson($oauth2Url);

    //第二步:根据全局access_token和openid查询用户信息  
    $access_token = $oauth2["access_token"];  
    $openid = $oauth2['openid'];  
    $get_user_info_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";
    $userinfo = getJson($get_user_info_url

//打印用户信息
  print_r($userinfo);
function getJson($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    return json_decode($output, true);
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值