首页 / PHP / 微信pc网页扫码授权登录php
微信pc网页扫码授权登录php
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了微信pc网页扫码授权登录php,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3820字,纯文字阅读大概需要6分钟。
内容图文
一、获取扫码登录授权二维码
html部分
<div id="login_container"></div>
<script src="http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js"></script>
<script>
var loginUrl = "http://" + window.location.host + "/index.php?route=common/home/weChatLogin";
//console.log(loginUrl);
var obj = new WxLogin({
id: "login_container",//存放二维码框
appid: "*************",
scope: "snsapi_login",
redirect_uri: encodeURIComponent(loginUrl),
state: Math.ceil(Math.random()*1000),
style: "black",
href: ""});
</script>
二、php部分.
class wxlogin
{
var $appid = "********";
var $appsecret = "******************";
//构造函数,获取Access Token
public function __construct($appid = NULL, $appsecret = NULL)
{
//扫码登录不需要该Access Token, 语义理解需要
//1. 本地写入
$res = @file_get_contents('test/access_token.json');
$result = json_decode($res, true);
$this->expires_time = $result["expires_time"];
$this->access_token = $result["access_token"];
if (time() > ($this->expires_time + 3600)) {
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $this->appid . "&secret=" . $this->appsecret;
$res = $this->http_request($url);
$result = json_decode($res, true);
$this->access_token = $result["access_token"];
$this->expires_time = time();
file_put_contents('test/access_token.json', '{"access_token": "' . $this->access_token . '", "expires_time": ' . $this->expires_time . '}');
}
}
//生成扫码登录的URL
public function qrconnect($redirect_url, $scope, $state = NULL)
{
$url = "https://open.weixin.qq.com/connect/qrconnect?appid=" . $this->appid . "&redirect_uri=" . urlencode($redirect_url) . "&response_type=code&scope=" . $scope . "&state=" . $state . "#wechat_redirect";
return $url;
}
//生成OAuth2的Access Token
public function oauth2_access_token($code)
{
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $this->appid . "&secret=" . $this->appsecret . "&code=" . $code . "&grant_type=authorization_code";
$res = $this->http_request($url);
return json_decode($res, true);
}
//获取用户基本信息(OAuth2 授权的 Access Token 获取 未关注用户,Access Token为临时获取)
public function oauth2_get_user_info($access_token, $openid)
{
$url = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $access_token . "&openid=" . $openid . "&lang=zh_CN";
$res = $this->http_request($url);
return json_decode($res, true);
}
public function http_request($url){
return file_get_contents($url);
}
}
//微信扫码登录
public function weChatLogin(){
$weixin = getModel("wxlogin");
//$weixin = new class_weixin();
if (!isset($_GET["code"])){
$redirect_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$jumpurl = $weixin->qrconnect($redirect_url, "snsapi_login", "123");
Header("Location: $jumpurl");
}else{
//获取token
$oauth2_info = $weixin->oauth2_access_token($_GET["code"]);
//获取用户信息
$userinfo = $weixin->oauth2_get_user_info($oauth2_info['access_token'], $oauth2_info['openid']);
//参数样式$userinfo用户信息结构
//array(10) { ["openid"]=> string(28) "oJDSV0lWOjfXE7VXk6C4_QuiUtQY" ["nickname"]=> string(6) "俊哥" ["sex"]=> int(1) ["language"]=> string(5) "zh_CN" ["city"]=> string(6) "成都" ["province"]=> string(6) "四川" ["country"]=> string(6) "中国" ["headimgurl"]=> string(129) "http://thirdwx.qlogo.cn/mmopen/vi_32/JuV6pCO123H8xHXGe7goSW9tFy0PKLL1zSH3uVuJ9QZ9omZeZ8TjfWGdtjtOBLAs82VqriajFecBys0pGjicVBow/132" ["privilege"]=> array(0) { } ["unionid"]=> string(28) "oAnnw0ix6eEYev5w7AUR29VoS-ow" }
//登录成功后跳转
header("Location:************");
}
}
内容总结
以上是互联网集市为您收集整理的微信pc网页扫码授权登录php全部内容,希望文章能够帮你解决微信pc网页扫码授权登录php所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。