微信 之网页第三方微信扫码登录
Posted yang-2018
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了微信 之网页第三方微信扫码登录相关的知识,希望对你有一定的参考价值。
一、方式一(网站内嵌二维码微信登录JS)
<?php /** * Created by PhpStorm. * User: 25754 * Date: 2019/6/4 * Time: 11:15 */ $state = md5("yangs"); include ‘./login.html‘;
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> #login_container position: absolute; left: 50%; margin-left: -150px; </style> </head> <body> <div id="login_container"></div> <script type="text/javascript" src="http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js"></script> <script> var obj = new WxLogin( self_redirect:true, id:"login_container", appid: "wx1851214902ef11bb", scope: "snsapi_login", redirect_uri: encodeURIComponent("http://www.boyuan.com/api/a/wx_third_login/login_third_weixin.php?action=do"), state: "$state", style: "", href: "" ); </script> </body> </html>
结果如图:
二、方式二(获取access_token)
<a href="/api/a/wx_third_login/login_third_weixin.php?action=init" target="_blank" class="wx" style="float:left"><i style="top:0;"></i>微信账号授权登录</a>
require DT_ROOT . ‘/api/a/wxopen.class.php‘; $wx = new WXOpen(); $state = md5("yangs"); switch ($action) case ‘init‘: $redirect_uri = "http://www.boyuan.com/api/a/wx_third_login/login_third_weixin.php?action=do"; $scope = ‘snsapi_login‘; $url = $wx->qrconnect($redirect_uri, $scope, $state); echo "<script>window.location.href=‘" . $url . "‘</script>"; break;
<?php /** * Created by PhpStorm. * User: 25754 * Date: 2019/6/4 * Time: 9:18 */ define(‘APPID‘, "wx1851214902ef11bb"); define(‘APPSECRET‘, ""); class WXOpen var $appid = APPID; var $appsecret = APPSECRET; //构造函数,获取Access Token public function __construct($appid = NULL, $appsecret = NULL) if ($appid && $appsecret) $this->appid = $appid; $this->appsecret = $appsecret; //生成扫码登录的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); //HTTP请求(支持HTTP/HTTPS,支持GET/POST) protected function http_request($url, $data = null) $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($data)) curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); $output = curl_exec($curl); curl_close($curl); return $output;
结果如图:
以上是关于微信 之网页第三方微信扫码登录的主要内容,如果未能解决你的问题,请参考以下文章