PHP实现微信开放平台扫码登录源码下载

简介: 演示下载可以参考这里:http://www.erdangjiade.com/php/1034.

演示下载可以参考这里:http://www.erdangjiade.com/php/1034.html

效果图如下:扫一下快速登陆


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

<!DOCTYPE html> 
<html> 
    <head> 
        <meta http-equiv="content-type" content="text/html;charset=utf-8"> 
    </head> 
    <body> 
        <span id="login_container"></span> 
        <script src="http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js"></script> 
        <script> 
            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: ""}); 
        </script> 
    </body> 
</html>

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); 
    }
相关文章
|
2月前
|
安全 关系型数据库 MySQL
PHP手术麻醉临床信息管理系统源码 术前、术中、术后全流程管理
麻醉文书是用于管理患者手术全过程的文书。 麻醉同意:医院麻醉知情同意书的填写和编辑; 病案查询:可以通过搜索框搜索患者查询病案; 申请手术:手术申请单的填写和打印; 风险评估:医院手术风险评估表的书写和编辑。
40 0
|
2月前
|
安全 前端开发 JavaScript
PHP医院信息化B/S手麻系统源码
开发语言:PHP、 js 技术架构:mysql+laravel+vue2 开发工具:oh-storm 前端框架:vue2 element 后端框架:laravel 数 据 库:mysql 8.0
25 0
PHP医院信息化B/S手麻系统源码
|
1月前
|
小程序 JavaScript 前端开发
基于微信小程序的商城购物系统的设计与实现(论文+源码)_kaic
基于微信小程序的商城购物系统的设计与实现(论文+源码)_kaic
|
10天前
|
PHP
php下载MP4视频
php下载MP4视频
9 0
|
12天前
|
人工智能 小程序 Java
Java智慧校园系统源码 微信小程序+电子班牌
通过设备管理对百纳智慧校园的智慧班牌以及百纳智慧屏(校牌)进行统一集中式管理,支持浏览所有设备的基本信息以及在离线状态,支持添加设备、设备一键开关机、一键重启、设置节假日开关机时间、设置日常开关机时间、远程班牌截屏、远程班牌升级等操作。
|
1月前
|
小程序
微信小程序下载代码
微信小程序下载代码
|
1月前
|
小程序 搜索推荐 算法
微信小程序外卖管理的设计与实现(论文+源码)_kaic
微信小程序外卖管理的设计与实现(论文+源码)_kaic
|
1月前
|
监控 小程序 前端开发
基于微信小程序充电桩预约管理系统的设计与实现(论文+源码)_kaic
基于微信小程序充电桩预约管理系统的设计与实现(论文+源码)_kaic
|
1月前
|
小程序 安全 前端开发
基于微信小程序的英语单词记忆系统的设计与实现(论文+源码)_kaic
基于微信小程序的英语单词记忆系统的设计与实现(论文+源码)_kaic
|
1月前
|
关系型数据库 MySQL Java
微信仿真平台的设计和实现(设计+源码)_kaic
微信仿真平台的设计和实现(设计+源码)_kaic