您好,欢迎访问一九零五行业门户网

php实现微信扫码登陆的思路与代码

最新微信扫码登陆源码演示下载这个是目前最常见的一个功能了,微信那么火爆,只有把它与自己的网站相结合,才能会有更多的用户注册,今天就来研究下微信扫码的功能,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); }
以上就是php实现微信扫码登陆的思路与代码的详细内容。
其它类似信息

推荐信息