最新赞助活动温馨提示:自愿赞助服务器费用,学生和没有工作的整站资源免费下载!
头像

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

来源:www.erdangjiade.com 沐浴春风 2016-08-06 07:42浏览(23256)

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

0、请不要问“在不在”之类的问题,有问题直接问!1、学生或暂时没有工作的童鞋,整站资源免费下载!2、¥9.9充值终身VIP会员,加我微信,826096331 拉你进VIP群学习!3、程序员加油,技术改变世界。 在线 充值

PHP实现微信开放平台扫码登录源码下载
分类:PHP > 微信 难易:
查看演示 下载资源:

加我微信,拉你进VIP群学习:

下载资源 下载积分: 260 积分

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);
    }
声明:本文为原创文章,如需转载,请注明来源erdangjiade.com并保留原文链接:https://www.erdangjiade.com/php/1034.html
评论80
头像

友情提示:垃圾评论一律封号 加我微信:826096331拉你进VIP群学习群

  • 头像 79楼
    10-19 11:51
    kkmllj2
    可包含后台账号绑定部分的代码
  • 头像 78楼
    03-18 00:19
    antoni
    学生怎么免费下载啊<br />
  • 头像 77楼
    09-16 17:41
    临颍人才网-客户经理-谷松凯
    有用吗 有用吗 有用吗 有用吗 有用吗 有用吗
  • 头像 76楼
    09-16 17:23
    临颍人才网-客户经理-谷松凯
    有用吗 有用吗有用吗有用吗有用吗
  • 头像 75楼
    09-09 09:01
    遇见
    下载了,白浪费积分!!不能用!!!
    1
    weidao

    有用了吗?

    2
    weidao

    交流一下,我帮你解决让有用,加一下QQ:6277979

  • 头像 74楼
    09-07 09:02
    遇见
    谁下载过?能用不?
  • 头像 73楼
    07-31 08:42
    wukezhiyi
    学生怎么免费下载啊
  • 头像 72楼
    07-22 16:00
    A店铺装修小程序网站开发推广
    学生或暂时没有工作的童鞋,整站资源免费下载!怎么免费下载?是需要先充值吗
  • 头像 71楼
    07-22 15:47
    A店铺装修小程序网站开发推广
    2、学生或暂时没有工作的童鞋,整站资源免费下载!怎么免费下载?
  • 头像 70楼
    07-22 15:43
    A店铺装修小程序网站开发推广
    充值 成VIP会员后可以下载不
1 2