微信授权网页扫码登录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)
上一篇 2022年6月3日 下午12:00
下一篇 2022年6月3日 下午12:00


相关推荐

  • 遗传算法——matlab实现

    遗传算法——matlab实现遗传算法

    2026年3月17日
    2
  • python深拷贝和浅拷贝的区别_Python3之深拷贝和浅拷贝区别

    python深拷贝和浅拷贝的区别_Python3之深拷贝和浅拷贝区别首先 我们知道 Python3 中 有 6 个标准的数据类型 他们又分为可以变和不可变 不可变 Number 数字 String 字符串 Tuple 元组 可以变 List 列表 Dictionary 字典 Set 集合 浅拷贝 copy 模块里面的 copy 方法实现 浅拷贝后 改变原始对象中为可变类型的元素的值 会同时影响拷贝对象 改变原始对象中为不可变类型的元素的值 不会响拷贝对象 代码演示 im

    2026年3月16日
    2
  • 补码计算方法

    补码计算方法很基础的知识 但是确实很多人并不知道怎么计算 总结了下 可以收藏 需要的时候看下 与编程息息相关学习目标 在 vc 6 0 中 int 类型所能存储的数字的范围是多少 int 类型所能存储的最大正数用十六进制表示 7FFFFFFFint 类型所能存储的绝对值最大的负数用十六进制表示是 8

    2026年3月19日
    3
  • sorl学习

    sorl学习solr nbsp 企业站内搜索配置分词器配置自定义域

    2026年3月10日
    3
  • MyBatis-plus的IPage分页

    MyBatis-plus的IPage分页单表的查询并根据时间分页 1 yml 或者 properties 中配数据源 配自己的数据库就好 mybatis plus mapper locations mapper xmlconfigura log impl org apache ibatis logging stdout StdOutImpl 这个是为了在控制台打印 SQL 语句 2 导入 Mybatis Plus 的依赖 我这里使用的是 3 4 0 的版本 这里一定要注意依赖的版本号 3 4 0 以后

    2026年3月17日
    2
  • cmd下出现找不到或无法加载主类的问题

    cmd下出现找不到或无法加载主类的问题之前安装过 jdk 安装另一个版本的 jdk 配置环境后出现了找不到或无法加载主类的问题 经过相关资料查询 整理后有如下情况 其实这些情况都不是我遇到的 我遇到的写在最后 1 环境变量配置错误 如果 java 和 javac 指定能够顺利运行 cmd 中没有出现无法识别相关指令之类的提示 说明 path 路径配置正确 此时 运行 javac 后成功生成 class 文件 运行 java 类名时出现提示找不到或无法加载主类 说明 classpath 路径错误 classpath 是运行时搜索类的路径 包括 jdk 中的类的路径

    2025年12月7日
    7

发表回复

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

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