マイクロチャネルのマイクロチャンネルスキャンコードログインのサードパーティのWeb

まず、方法(二次元コード埋め込みマイクロチャネルのウェブサイトのログインJS)

<?PHP
 / * * 
 * PhpStormによって作成されます。
 *ユーザー:25754 
 *発売日:2019年6月4日
 *時間:11時15分
 * / 

$状態 = MD5( "yangs" ); 

などが './login.htmlを';
<!DOCTYPE HTML > 
< HTML のlang = "EN" > 
< ヘッド> 
    < メタのcharset = "UTF-8" > 
    < タイトル>タイトル</ タイトル> 
    < スタイル> 
        #login_container { 
            位置絶対50%; 
            margin-left -150px ; 
        } 
    </ スタイル> 
</ ヘッド> 
<> 
< DIV ID = "login_container" > </ divの> 
< スクリプトタイプ= "テキスト/ javascriptの" SRC = "http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js" > </ スクリプト> 
< スクリプト> 
    VAR OBJ =  新しいWxLogin({ 
        self_redirect:
        ID:" login_container " 
        APPID:" wx1851214902ef11bb " 
        スコープ:"snsapi_login 
        REDIRECT_URI:encodeURIComponentで(" http://www.boyuan.com/api/a/wx_third_login/login_third_weixin.php?action=do " )、
        状態:" {$状態} " 
        スタイル:"" 
        のhref:"" 
    } ); 
</ スクリプト> 
</ ボディ> 
</ 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;
    }
}

 

结果如图:

 

おすすめ

転載: www.cnblogs.com/yang-2018/p/10972765.html
おすすめ