当前位置:Gxlcms > PHP教程 > php实现微信扫码登陆的思路与代码

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

时间:2021-07-01 10:21:17 帮助过:38人阅读

最新微信扫码登陆源码演示下载这个是目前最常见的一个功能了,微信那么火爆,只有把它与自己的网站相结合,才能会有更多的用户注册,今天就来研究下微信扫码的功能,1、首先到微信开放平台申请https://open.weixin.qq.com/ 获取到appid和APPSECRET,前台显示页面如下
完整代码如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="content-type" content="text/html;charset=utf-8">
  5. </head>
  6. <body>
  7. <span id="login_container"></span>
  8. <script src="http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js"></script>
  9. <script>
  10. var obj = new WxLogin({
  11. id: "login_container",
  12. appid: "wxed782be999f86e0e",
  13. scope: "snsapi_login",
  14. redirect_uri: encodeURIComponent("http://" + window.location.host + "/login.php"),
  15. state: Math.ceil(Math.random()*1000),
  16. style: "black",
  17. href: ""});
  18. </script>
  19. </body>
  20. </html>
  21. 2、PHP处理代码页面
  22. /*
  23. require_once('weixin.class.php');
  24. $weixin = new class_weixin();
  25. */
  26. define('APPID', "wx19ba77624e083e08");
  27. define('APPSECRET', "c1a56a5c4247dd44c320c9719c5ceb90");
  28. class class_weixin
  29. {
  30. var $appid = APPID;
  31. var $appsecret = APPSECRET;
  32. //构造函数,获取Access Token
  33. public function __construct($appid = NULL, $appsecret = NULL)
  34. {
  35. if($appid && $appsecret){
  36. $this->appid = $appid;
  37. $this->appsecret = $appsecret;
  38. }
  39. //扫码登录不需要该Access Token, 语义理解需要
  40. //1. 本地写入
  41. $res = file_get_contents('access_token.json');
  42. $result = json_decode($res, true);
  43. $this->expires_time = $result["expires_time"];
  44. $this->access_token = $result["access_token"];
  45. if (time() > ($this->expires_time + 3600)){
  46. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->appsecret;
  47. $res = $this->http_request($url);
  48. $result = json_decode($res, true);
  49. $this->access_token = $result["access_token"];
  50. $this->expires_time = time();
  51. file_put_contents('access_token.json', '{"access_token": "'.$this->access_token.'", "expires_time": '.$this->expires_time.'}');
  52. }
  53. }
  54. /*
  55. * PART1 网站应用
  56. */
  57. /*
  58. header("Content-type: text/html; charset=utf-8");
  59. require_once('wxopen.class.php');
  60. $weixin = new class_weixin();
  61. if (!isset($_GET["code"])){
  62. $redirect_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
  63. $jumpurl = $weixin->qrconnect($redirect_url, "snsapi_login", "123");
  64. Header("Location: $jumpurl");
  65. }else{
  66. $oauth2_info = $weixin->oauth2_access_token($_GET["code"]);
  67. $userinfo = $weixin->oauth2_get_user_info($oauth2_info['access_token'], $oauth2_info['openid']);
  68. var_dump($userinfo);
  69. }
  70. */
  71. //生成扫码登录的URL
  72. public function qrconnect($redirect_url, $scope, $state = NULL)
  73. {
  74. $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";
  75. return $url;
  76. }
  77. //生成OAuth2的Access Token
  78. public function oauth2_access_token($code)
  79. {
  80. $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid."&secret=".$this->appsecret."&code=".$code."&grant_type=authorization_code";
  81. $res = $this->http_request($url);
  82. return json_decode($res, true);
  83. }
  84. //获取用户基本信息(OAuth2 授权的 Access Token 获取 未关注用户,Access Token为临时获取)
  85. public function oauth2_get_user_info($access_token, $openid)
  86. {
  87. $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
  88. $res = $this->http_request($url);
  89. return json_decode($res, true);
  90. }

以上就是php实现微信扫码登陆的思路与代码的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行