Home  >  Article  >  Backend Development  >  PHP ideas and code to implement WeChat code scanning login

PHP ideas and code to implement WeChat code scanning login

PHP中文网
PHP中文网Original
2017-09-01 16:59:572399browse

The latest WeChat scan code login source code demo download is currently the most common function. WeChat is so popular. Only by combining it with your own website can more users register. Let’s study WeChat today. To scan the QR code, 1. First apply to the WeChat open platform https://open.weixin.qq.com/ to obtain the appid and APPSECRET. The front-end display page is as follows
The complete code is as follows:

<!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(&#39;weixin.class.php&#39;); 
    $weixin = new class_weixin(); 
*/ 
 
define(&#39;APPID&#39;,        "wx19ba77624e083e08"); 
define(&#39;APPSECRET&#39;,    "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(&#39;access_token.json&#39;); 
        $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(&#39;access_token.json&#39;, &#39;{"access_token": "&#39;.$this->access_token.&#39;", "expires_time": &#39;.$this->expires_time.&#39;}&#39;); 
        } 
    } 
 
    /* 
    *  PART1 网站应用 
    */ 
 
    /* 
    header("Content-type: text/html; charset=utf-8"); 
    require_once(&#39;wxopen.class.php&#39;); 
    $weixin = new class_weixin(); 
    if (!isset($_GET["code"])){ 
        $redirect_url = &#39;http://&#39;.$_SERVER[&#39;HTTP_HOST&#39;].$_SERVER[&#39;REQUEST_URI&#39;]; 
        $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[&#39;access_token&#39;], $oauth2_info[&#39;openid&#39;]); 
        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); 
    }


The above is the detailed content of PHP ideas and code to implement WeChat code scanning login. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn