微信授权网页扫码登录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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • python解决约瑟夫环问题(容易理解版)「建议收藏」

    python解决约瑟夫环问题(容易理解版)「建议收藏」python解决约瑟夫环问题(容易理解版)约瑟夫环问题:已知n个人(以编号1,2,3…n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数,数到k的那个人被杀掉;他的下一个人又从1开始报数,数到k的那个人又被杀掉;依此规律重复下去,直到圆桌周围的人只剩最后一个。第一次写博客,请大家多多指教。超级容易理解版:思路:刚开始把所有的人放到一个列表里面去,报的数字不是3就把这个人放到列表的最后一…

    2022年6月4日
    34
  • phpStudy本地环境测试,打开网页很慢的解决办法!

    phpStudy本地环境测试,打开网页很慢的解决办法!

    2021年10月16日
    45
  • 常用链表排序算法_单链表的排序算法

    常用链表排序算法_单链表的排序算法转载自:http://blog.csdn.net/northplayboy/article/details/552388  ========================== 功能:选择排序(由小到大) 返回:指向链表表头的指针==========================*//* 选择排序的基本思想就是反复从还未排好序的那些节点中, 选出键值(就是

    2022年10月11日
    3
  • C# TransactionScope「建议收藏」

    C# TransactionScope「建议收藏」TransactionScopeTransactionScope事务处理经常用到,老是用了又忘,做点记录。TransactionScope的定义跟使用介绍。TransactionScopeOptionTransactionScopeOption枚举型用来决定一个TransactionScope是用已有的事务,还是定义TransactionScope的新做一个事务,还是完全不用事务。默认是Required,Required表示如果已有事务,就加入该事务,否则新建一个事务。TransactionOp

    2022年7月19日
    20
  • Java SoftReference

    Java SoftReferenceSoftReference的语义就是当内存不够用的时候,GC会回收SoftReference所引用的对象。所以,在memorysensitive的程序中将某些大型数据设置成SoftReference再合适不过了。创建一个SoftReference:[code="java"]Objectobj=newObject();SoftReferencesoftRef=…

    2025年10月8日
    3
  • Gson用法: json转对象

    Gson用法: json转对象1 创建 Gson Gsongson newGsonBuild setPrettyPri create 或者 Gsongson newGson 2 json 转对象 Objectobj gson fromJson jsonString Object class 或者 Obje

    2025年10月10日
    6

发表回复

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

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