微信授权网页扫码登录php,PHP实现微信开放平台扫码登录源码

微信授权网页扫码登录php,PHP实现微信开放平台扫码登录源码1、首先到微信开放平台申请https://open.weixin.qq.com/获取到appid和APPSECRET,前台显示页面如下html>varobj=newWxLogin({id:”login_container”,appid:”wxed782be999f86e0e”,scope:”snsapi_login”,redirect_uri:encodeURICompon…

大家好,又见面了,我是你们的朋友全栈君。

1、首先到微信开放平台申请https://open.weixin.qq.com/ 获取到appid和APPSECRET,前台显示页面如下html>

var obj = new WxLogin({

id: “login_container”,

appid: “wxed782be999f86e0e”,

scope: “snsapi_login”,

redirect_uri: encodeURIComponent(“http://” + window.location.host + “/login.php”),

state: Math.ceil(Math.random()*1000),

style: “black”,

href: “”});

2、PHP处理代码页面/*

require_once(‘weixin.class.php’);

$weixin = new class_weixin();

*/

define(‘APPID’,        “wx19ba77624e083e08”);

define(‘APPSECRET’,    “c1a56a5c4247dd44c320c9719c5ceb90”);

class class_weixin

{

var $appid = APPID;

var $appsecret = APPSECRET;

//构造函数,获取Access Token

public function __construct($appid = NULL, $appsecret = NULL)

{

if($appid && $appsecret){

$this->appid = $appid;

$this->appsecret = $appsecret;

}

//扫码登录不需要该Access Token, 语义理解需要

//1. 本地写入

$res = file_get_contents(‘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(‘access_token.json’, ‘{“access_token”: “‘.$this->access_token.'”, “expires_time”: ‘.$this->expires_time.’}’);

}

}

/*

*  PART1 网站应用

*/

/*

header(“Content-type: text/html; charset=utf-8”);

require_once(‘wxopen.class.php’);

$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{

$oauth2_info = $weixin->oauth2_access_token($_GET[“code”]);

$userinfo = $weixin->oauth2_get_user_info($oauth2_info[‘access_token’], $oauth2_info[‘openid’]);

var_dump($userinfo);

}

*/

//生成扫码登录的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);

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/136572.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 因果图-判定表法

    因果图-判定表法一、应用场合界面中有多个控件,控件之间存在组合和限制关系,不同输入条件组合会对应不同的输出结果,为了理清每种输入条件组合和输出结果之间的对应关系,可以使用因果图/判定表法。注意:因果图/判定表法适合测试组合数量较少的情况,如果组合数量较多时,适合使用正交排列法。(更高效)二、因果图法基础1、因果图法因:输入条件果:输出结果因果图法:用画图的方式表示输入条件(因)和输出结果(果)之间的关系。2、图形符号(了解)…

    2022年8月14日
    3
  • 实现财务自由之路_程序员的个人财富课

    实现财务自由之路_程序员的个人财富课给自己设定一个目标

    2022年10月7日
    2
  • ModuleNotFoundError: No module named ‘_bz2‘问题解决「建议收藏」

    ModuleNotFoundError: No module named ‘_bz2‘问题解决「建议收藏」在运行PyTorch代码的时候,报了“ModuleNotFoundError:Nomodulenamed’_bz2’”错误,完整报错提示信息如下:Traceback(mostrecentcalllast):File”stat_model.py”,line1,in<module>fromtorchstatimportstatFile”/usr/local/lib/python3.7/site-packages/torchstat/__i

    2022年6月6日
    496
  • ORACLE sqlplus连接报ORA-12560解决办法

    ORACLE sqlplus连接报ORA-12560解决办法在windows服务器上cmdsqlplus用户名/密码连接报ORA-12560(协议适配器错误啥的)解决办法如下:1.再原命令上增加@127.0.0.1/实例sqlplus用户名/密码@127.0.0.1/实例assysdba(assysdba可不加)尝试是否能连接。若不能试试cmd打开后先setoracle_sid=实例再…

    2022年7月16日
    16
  • 免费mac激活码2021【2021免费激活】

    (免费mac激活码2021)本文适用于JetBrains家族所有ide,包括IntelliJidea,phpstorm,webstorm,pycharm,datagrip等。https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~M…

    2022年3月20日
    53
  • visdom 使用教程

    visdom 使用教程visdom教程visdom安装与启动服务visdom常用功能image窗口:图像显示与更新窗口显示images窗口:多个图像显示与更新窗口显示text窗口:显示文本与更新文本line窗口:绘制折线图与更新折线图scatter窗口:绘制散点图与更新散点图visdom安装与启动服务安装visdompipinstallvisdom打开服务python-mvisdom.server…

    2022年6月24日
    32

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号