php实现微信扫码登陆,PHP实现微信开放平台扫码登录源码下载[通俗易懂]

php实现微信扫码登陆,PHP实现微信开放平台扫码登录源码下载[通俗易懂]1、首先到微信开放平台申请https://open.weixin.qq.com/获取到appid和APPSECRET,前台显示页面如下varobj=newWxLogin({id:”login_container”,appid:”wxed782be999f86e0e”,scope:”snsapi_login”,redirect_uri:encodeURIComponent(“htt…

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

38b302dcf26f3ce05e440083d5575dd9.png

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

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’,        “”);

define(‘APPSECRET’,    “”);

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://open.weixin.qq.com

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

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

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


相关推荐

  • 查询端口下请求总数

    查询端口下请求总数

    2021年9月8日
    63
  • POJ2029——Get Many Persimmon Trees

    POJ2029——Get Many Persimmon Trees

    2022年1月4日
    40
  • 详解银行家算法「建议收藏」

    详解银行家算法「建议收藏」简述:银行家算法是一种可以避免死锁的方法就是以银行借贷系统的分配策略为基础,判断并保证系统的安全运行。我们可以把操作系统看作是银行家,操作系统管理的资源相当于银行家管理的资金,进程向操作系统请求分配资源就相当于用户向银行家贷款。实现方法:为保证资金的安全,银行家规定:(1)当一个顾客对资金的最大需求量不超过银行家现有的资金时就可接纳该顾客;(即当资源池中剩余的可利用资源>=线…

    2022年7月22日
    11
  • CentOS如何查看本机ip

    CentOS如何查看本机ip之前我们习惯的 ipconfig 不能用了原来是改了呀 改成了 ipaddress 我们可以输入 ipaddress 也可以输入 ipaddr 或者是 ipadd 你会发现 无法查看 ip 但至少这个命令是存在的你需要打开网卡配置文件 etc sysconfig network scripts ifcfg ens33 将 ONBOOT 修改为 yes 也就是启动网卡 vi et

    2025年10月10日
    4
  • 常用正则表达式[通俗易懂]

    常用正则表达式[通俗易懂]常用正则表达式

    2022年4月23日
    51
  • 在虚拟机上安装使用LoadRunner教程

    在虚拟机上安装使用LoadRunner教程记录一下我的安装LoadRunner血泪史1.LoadRunner11在win10上使用总是出问题,后来看到只能在win7在用,就在VMware建了个win7镜像,在msdn(https://msdn.itellyou.cn/)上下了win7的cn_windows_7_enterprise_x64_dvd_x15-70741.iso,后来安装VMwareTools的时候会报“安装程序无法继续。本程序需要您将此虚拟机上安装的操作系统更新到SP1”这个才是能用的镜像:cn_windows_7_enter

    2022年5月23日
    92

发表回复

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

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