关于获取QQ头像接口 QQ头像地址加密方法 附完整API接口代码

前言

在制作网站的时候,有时候评论要使用到头像,那么最直接的方法就是用户输入QQ直接获取指定的头像
腾讯QQ头像获取直链api:
API接口:http://q1.qlogo.cn/g?b=qq&nk=QQ号码&s=640
调用例子:http://q1.qlogo.cn/g?b=qq&nk=10001&s=640
但是这样会暴露QQ,懂点代码的直接查看图片地址,就把QQ暴露了,这样对用户隐私不好
图片[1]-关于获取QQ头像接口 QQ头像地址加密方法 附完整API接口代码-一只薛眠羊
于是想到了这样的QQ头像地址

https://q1.qlogo.cn/g?b=qq&k=Vjic48anMfN6ovAxw4eN94w&s=100

上面的地址,中没有QQ号,却获取了某人的qq头像,里面的重要参数就是K后面的值,那么如何获取K值呢?
接下来小薛教大家如何获取!

第一种方法

您可以直接访问https://api.xuemy.cn/doc/qqtx 完成在线调用

第二种方法:

获取QQ头像不暴露QQ接口直接调用

https://s.p.qq.com/pub/get_face?img_type=3&uin=QQ

将上面的QQ 换成要调用的QQ 参数即可

如要调用10001的头像:

https://s.p.qq.com/pub/get_face?img_type=3&uin=10001

第三种方法:

访问下面的地址就能得到一个json

http://ptlogin2.qq.com/getface?appid=101382166&imgtype=1&encrytype=0&devtype=0&keytpye=0&uin=QQ号码&r=0.17780657206333406

简化下无用参数,变成

http://ptlogin2.qq.com/getface?&imgtype=1&uin=QQ号

访问上述地址得到的json

pt.setHeader({"10001":"https://thirdqq.qlogo.cn/g?b=sdk&k=Vjic48anMfN6ovAxw4eN94w&s=100&t=1555323598"})

知道怎么获取就好处理了,上全世界最好的语言php

$qq = '你的QQ号码';
$geturl = 'http://ptlogin2.qq.com/getface?&imgtype=1&uin='.$qq;
$qquser = file_get_contents($geturl);
$str1 = explode('&k=', $qquser);
$str2 = explode('&s=', $str1[1]);
$k = $str2[0];
$qqimg = 'https://q1.qlogo.cn/g?b=qq&k='.$k.'&s=100';
echo $qqimg

完整版API代码

使用说明

您先新建一个后缀为.txt文件,将下列代码复制粘贴至新建的txt文档内,然后保存后退出,将.txt后缀修改为.php,上传至您的服务器,然后调用参数http://127.0.0.1/?qq=QQ号码,然后就可以直接获取到加密头像的链接啦!

/*
 * @Author: 一只薛眠羊
 * @Date: 2023-01-17 15:45:21
 * @LastEditors: 一只薛眠羊
 * @LastEditTime: 2022-11-02 13:52:21
 * @FilePath: \WWW\qqtx.php
 * @NetWork: 一只薛眠羊 www.xuemy.cn
 */
<?php
error_reporting(E_ALL || ~E_NOTICE);    //禁止显示PHP错误信息
class Api
{
    public static function get_curl($url,$header = 0, $ua = 0, $nobaody = 0)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $clwl[] = "Accept:*/*";
        $clwl[] = "Accept-Encoding:gzip,deflate,sdch";
        $clwl[] = "Accept-Language:zh-CN,zh;q=0.8";
        curl_setopt($ch, CURLOPT_HTTPHEADER, $clwl);
        if ($header) {
            curl_setopt($ch, CURLOPT_HEADER, TRUE);
        }
        if ($ua) {
            curl_setopt($ch, CURLOPT_USERAGENT, $ua);
        } else {
            curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Linux; U; Android 4.0.4; es-mx; HTC_One_X Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0');
        }
        if ($nobaody) {
            curl_setopt($ch, CURLOPT_NOBODY, 1);
            //主要头部
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //跟随重定向
        }
        curl_setopt($ch, CURLOPT_ENCODING, "gzip");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $ret = curl_exec($ch);
        curl_close($ch);
        return $ret;
    }
    /**
     * @param Json信息输出
     * @arr:需要转换的数组
     */
    public static function json($arr = array(), $code = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
    {
        header("Content-Type:text/json");
        header("Access-Control-Allow-Origin: *");
        exit(json_encode($arr, $code));
    }
    /**
     * 获取GET POST表单数据
     */
    public static function GetData($key = "")
    {
        if (empty($key)) {
            return $_REQUEST;
        }
        if (isset($_REQUEST[$key])) {
            return $_REQUEST[$key];
        } else {
            return null;
        }
    }
}
$qquin = Api::GetData('qq'); //qq参数
if (!empty($qquin)) {
    $size = Api::GetData('size');
    if ($size == 640) {
        $add = Api::get_curl('https://ptlogin2.qq.com/getface?uin=' . $qquin . '&imgtype=3');
        $preg = '#https://.*s=100#'; //使用正则匹配图像URL
        preg_match($preg, $add, $match);
        //var_dump($match);
        $imgurl = $match['0'];
        $deimg = str_replace("s=100", "s=640", $imgurl); //替换图片尺寸
        $qrand = 'q' . rand(1, 4);
        $img = str_replace("thirdqq", $qrand, "$deimg"); //替换域名前缀
        Api::json(['code' => 200, 'msg' => 'success', 'imgurl' => $img]);
    } else {
        $add = Api::get_curl('https://ptlogin2.qq.com/getface?uin=' . $qquin . '&imgtype=3');
        $preg = '#https://.*s=100#'; //使用正则匹配图像URL
        preg_match($preg, $add, $match);
        //var_dump($match);
        $imgurl = $match['0'];
        $qrand = 'q' . rand(1, 4);
        $img = str_replace("thirdqq", $qrand, $imgurl); //替换域名前缀
        Api::json(['code' => 200, 'msg' => 'success', 'imgurl' => $img]);
    }
} else {
    Api::json(['code' => -1, 'msg' => 'errors', 'imgurl' => null]);
}
?>

需要您先回复此文章,然后刷新本页即可获取代码!

© 版权声明
THE END
喜欢就支持一下吧
点赞11赞赏 分享
评论 共4条

请登录后发表评论

    • 头像消失的可乐云0