MENU

微信扫码登录详解-可做登录授权等

• February 9, 2020 • Read: 3572 • 网站源码

之前写了一篇QQ扫码登录,现在给微信的也给更了吧,直接上代码,不懂得评论区问我。

<?php
header('Access-Control-Allow-Origin:*');
header('Content-type:application/json; charset=utf-8');
error_reporting(0);
date_default_timezone_set("PRC");
!empty($_GET['type']) ? $type = $_GET['type'] : error("请求参数错误,请刷新重试!~~");
switch ($type)
{
    case 'Getqrcode':
        echo json_encode(getqrcode());
        break;
    case 'Getresult':
        !empty($_GET['qrsig']) ? $qrsig=$_GET['qrsig'] : error("请求参数错误,缺少qrsig~~");
        echo json_encode(getresult($qrsig),JSON_UNESCAPED_UNICODE);
        break;
    default:
        echo json_encode(getqrcode());
}
/**
 * 获取二维码
 */
function getqrcode() {
    $api = 'https://login.wx2.qq.com/jslogin?appid=wx782c26e4c19acffb';
    $ret = get_curl($api);
    preg_match('/"(.*?)"/', $ret, $matches);
    $qrcode['data'] = 'https://login.weixin.qq.com/l/'.$matches[1];
    $qrcode['uuid'] = $matches[1];
    return $qrcode;
}

/**
 * @param $qrsig
 * @return array
 * 获取登录状态
 */
function getresult($uuid) {
    $paras['ctime'] = 1000;
    $paras['rtime'] = 1000;
    $paras['refer'] = 'https://wx2.qq.com/';
    $api = 'https://login.wx2.qq.com/cgi-bin/mmwebwx-bin/login?loginicon=true&uuid=' . $uuid . '&tip=0';
    $body = get_curl($api, $paras);
    preg_match('/(\d){3}/', $body, $code);
    preg_match('/redirect_uri="(.*?)"/', $body, $url);
    if ($code[0] == '200') {
        $body = get_curl($url[1]);
        preg_match('/<wxuin>(\d*?)<\/wxuin>/', $body, $wxuin);
        $ret['code'] = 200;
        $ret['data']['uin'] = $wxuin[1];
        $ret['data']['type'] = 'wx';
        $ret['msg'] = '微信登录成功';
    } else {
        $ret['code'] = 408;
        $ret['msg'] = '请使用手机微信扫码登录';
    }
    return $ret;
}

function get_curl($url, $paras = array()) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $httpheader[] = "Accept:*/*";
    $httpheader[] = "Accept-Encoding:gzip,deflate,sdch";
    $httpheader[] = "Accept-Language:zh-CN,zh;q=0.8";
    $httpheader[] = "Connection:close";
    curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
    if ($paras['ctime']) { // 连接超时
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $paras['ctime']);
    }
    if ($paras['rtime']) { // 读取超时
        curl_setopt($ch, CURLOPT_TIMEOUT_MS, $paras['rtime']);
    }
    if ($paras['post']) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $paras['post']);
    }
    if ($paras['header']) {
        curl_setopt($ch, CURLOPT_HEADER, true);
    }
    if ($paras['cookie']) {
        curl_setopt($ch, CURLOPT_COOKIE, $paras['cookie']);
    }
    if ($paras['refer']) {
        if ($paras['refer'] == 1) {
            curl_setopt($ch, CURLOPT_REFERER, 'http://m.qzone.com/infocenter?g_f=');
        } else {
            curl_setopt($ch, CURLOPT_REFERER, $paras['refer']);
        }
    }
    if ($paras['ua']) {
        curl_setopt($ch, CURLOPT_USERAGENT, $paras['ua']);
    } else {
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
    }
    if ($paras['nobody']) {
        curl_setopt($ch, CURLOPT_NOBODY, 1);
    }
    curl_setopt($ch, CURLOPT_ENCODING, "gzip");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $ret = curl_exec($ch);
    curl_close($ch);
    return $ret;
}

function error($str){
    exit(json_encode([
        "code"=>-1,
        "msg"=>$str
    ],JSON_UNESCAPED_UNICODE));
}
Last Modified: February 12, 2020
Archives QR Code Tip
QR Code for this page
Tipping QR Code
Leave a Comment

3 Comments
  1. 先生你好,这是Getqrcode的响应:{"data":"https://login.weixin.qq.com/l/QZ8i_1pU9Q==","uuid":"QZ8i_1pU9Q=="}
    data的链接打开显示无法 无法识别的二维码,请指点

  2. {"code":200,"data":{"uin":null,"type":"wx"},"msg":"微信登录成功"}
    uin获取不到,网页登录的时候提示:为了你的帐号安全,此微信号不能登录网页微信。你可以使用Windows微信或Mac微信在电脑端登录

  3. 很好,我先借鉴一下,顺便看看QQ的....