MENU

QQ扫码登陆详解-可做登陆或者授权程序下载

• January 7, 2020 • Read: 4003 • 网站源码,代码分享,经验分享,原创作品

前言:

最近在写淘宝联盟吃佣金的程序,账号需要独立分配,注册啥的太麻烦了,就想着直接用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() {

$url='https://ssl.ptlogin2.qq.com/ptqrshow?appid=549000912&e=2&l=M&s=4&d=72&v=4&t=0.5409099'.time().'&daid=5';
    $arr=get_curl_split($url);
    preg_match('/qrsig=(.*?);/',$arr['header'],$match);
    if($qrsig=$match[1])
        return array('code'=>1,'qrsig'=>$qrsig,'data'=>base64_encode($arr['body']));
    else
        return array('code'=>-1,'msg'=>'二维码获取失败');

}


/**
 * @param $qrsig
 * @return array
 * 获取登录状态
 */
function getresult($qrsig) {
    $ret = array();
    $api = 'https://ssl.ptlogin2.qq.com/ptqrlogin?u1=' . urlencode('https://qzs.qzone.qq.com/') . '&ptqrtoken=' . getqrtoken($qrsig) . '&ptredirect=0&h=1&t=1&g=1&from_ui=1&ptlang=2052&action=0-1-' . time() . '&js_ver=90220&js_type=1&login_sig=&pt_uistyle=40&aid=549000912&daid=5&has_onekey=1';
    $paras['cookie'] = 'qrsig=' . $qrsig . ';';
    $body = get_curl($api, $paras);
    if (preg_match("/ptuiCB\('(.*?)'\)/", $body, $arr)) {
        $r = explode("','", str_replace("', '", "','", $arr[1]));
        if ($r[0] == 0) {
            preg_match('/uin=(\d+)&/', $body, $uin);
            $ret['code'] = 1;
            $ret['data']['uin'] = $uin[1];
            $ret['msg'] = 'QQ登录成功';
        } elseif ($r[0] == 65) {
            $ret['msg'] = '登录二维码已失效,请刷新重试!';
        } elseif ($r[0] == 66) {
            $ret['msg'] = '请使用手机QQ扫码登录';
        } elseif ($r[0] == 67) {
            $ret['msg'] = '正在验证二维码...';
        } else {
            $ret['msg'] = '未知错误001,请刷新重试!';
        }
    } else {
        $ret['msg'] = '未知错误002,请刷新重试!';
    }
    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;
}


/** QQ空间Token算法*/
function getqrtoken($qrsig) {
    $len = strlen($qrsig);
    $hash = 0;
    for ($i = 0; $i < $len; $i++) {
        $hash += (($hash << 5) & 2147483647) + ord($qrsig[$i]) & 2147483647;
        $hash &= 2147483647;
    }
    return $hash & 2147483647;
}

function error($str){
    exit(json_encode([
        "code"=>-1,
        "msg"=>$str
    ],JSON_UNESCAPED_UNICODE));
}

微信的也可以写的,抽空我也给发篇文章讲一下,最近不知道更新啥了,就随便水一水,提前祝大家新年快乐!

演示:

https://api.oioweb.cn/demo/qqqrcode.html

Last Modified: April 9, 2020
Archives QR Code Tip
QR Code for this page
Tipping QR Code
Leave a Comment

10 Comments
  1. BEAR BEAR

    我好像啥也没输出呢

  2. [...]该源代码来自教书先生博客 https://blog.oioweb.cn/index.php/archives/1307.html ,发现还挺好用的,于是自己改了下下代码。[...]

    1. @QQ扫码登陆详解 - 幸运柒可以@(大拇指)

  3. [...]本文根据根教书先生QQ空间扫码登录改写为QQ个人中心扫码登录,并进行了个人修改和优化 参考自:QQ 扫码登陆详解 – 可做登陆或者授权程序下载 – 教书先生个人博客[...]

  4. 大佬,扫码之后返回的参数有哪些,怎么取出来呀,看了你的演示站里的参数 在这个源码里取不到,空值

    1. @默默在返回消息头里面

  5. 我改进了一下,https://api.azyes.cn/Tencent/Api/GetCookie.html

    1. @阿柒扫码成功返回的参数怎么看,源码里没有找到呀,不会取

  6. 刚好需要,谢谢开源~

  7. 来个Java版的呗