QQ互联扫码登陆、微信扫码登录代码教程

QQ互联扫码登陆、微信扫码登录代码教程

作者 : 资源客 本文共6273个字,预计阅读时间需要16分钟 发布时间: 2020-04-1 共162人阅读

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() {
    $qrcode = array();
    $api = 'https://ssl.ptlogin2.qq.com/ptqrshow?appid=549000912&e=2&l=M&s=3&d=72&v=7&t=0.1415855' . time();
    $paras['header'] = 1;
    $ret = get_curl($api, $paras);
    preg_match('/qrsig=(.*?);/', $ret, $matches);
    preg_match_all('/ (\d){3}/', $ret, $Conlen);
    $arr = explode('com;', $ret);
    $qrcode['qrsig'] = $matches[1];
    $qrcode['data'] = base64_encode(trim($arr['1']));
    return $qrcode;
}
 
/**
 * @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));
}

<?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);
        //print_r($ret);die;
    preg_match('/"(.*?)"/', $ret, $matches);
        //print_r($matches);die;
    $qrcode['data'] = 'https://login.weixin.qq.com/qrcode/'.$matches[1];
    $qrcode['uuid'] = $matches[1];
    return $qrcode;
}
  
/**
 * [url=home.php?mod=space&uid=952169]@Param[/url] $qrsig
 * [url=home.php?mod=space&uid=155549]@Return[/url] 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('/(\d*?)<\/wxuin>/', $body, $wxuin);
        $ret['code'] = 200;
        $ret['data']['uin'] = $wxuin[1];
        $ret['data']['type'] = 'wx';
        $ret['msg'] = '微信登录成功';
    } else {
        $ret['code'] = 408;
        $ret['msg'] = '请使用手机微信扫码登录';
    }
        print_r($ret);die;
    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));
}
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请不要用于商业用途!
3. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"qq301.com",如遇到无法解压的请联系管理员!
资源客是一个优秀的分享资源站,本站资源均为各位友友分享而来,特殊原创会标明如有侵犯版权等可联系772955675@qq.com 删除

资源客 » QQ互联扫码登陆、微信扫码登录代码教程