zhenzi0322

测试

登录微信公众号》》公众号设置》》功能设置》》网页授权域名》》设置
在这里设置你网站的域名就可以了,然后下载txt文件放到网站的根目录下。
测试:http://域名/下载的文件名.txt,如下显示内容:

上面可以访问就行了。

静默授权

以下是采用PHP实现的。

控制器(Bargain)

控制器中的方法

wxindex

该方法是公共方法,内容如下:

$appid=\'wx97xxxxxxdc767\';
$redirect_uri = urlencode ( \'http://域名/块/Bargain/redirect_uria\' );
$url="https://open.weixin.qq.com/connect/oauth2/authorize?ppid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
header("Location:".$url);
getJson

该方法是私有方法,内容如下:

$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);
redirect_uria

该方法是公共方法,内容如下:

header("Content-Type: text/html;charset=utf-8");
$appid = "wx97exxxbdc767";  //APPID
$secret = "fdc4dxxxx27a63xxa805";  //AppSecret
$code = $_GET["code"];

// 1. 取全局access_token
$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
$token = $this->getJson($url);

// 2. 取得微信openid
$oauth2Url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
$oauth2 = $this->getJson($oauth2Url);

// 3. 根据全局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 = $this->getJson($get_user_info_url);

// 4. 打印用户信息
var_dump($userinfo);

注意事项

  • 调起wxindex方法时会自动跳转到redirect_uria方法里,进入redirect_uria会带有code参数,可以使用GET来接收。
  • 静默授权:snsapi_base ,没有弹窗,只能获取用户的openid
  • 非静默授权:snsapi_userinfo , 有弹框弹出需要用户手动点击确认授权。可以获取openid,用户的头像,昵称等。

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-10
  • 2021-11-17
  • 2022-01-08
  • 2022-02-08
  • 2022-12-23
猜你喜欢
  • 2021-05-10
  • 2022-12-23
  • 2022-01-16
  • 2022-02-18
  • 2021-07-16
相关资源
相似解决方案