Yesterday, someone in the group asked how the Dux theme directly obtains a name and avatar when a QQ number is entered in the comments section. I looked at the Dux theme and found that this feature was not integrated. However, the Dux theme for Emlog has this feature built in. I originally wanted to port it to WordPress, but I found that the comment modules of Emlog and WP are different, and I would have had to rewrite almost the entire style and JS to implement it, so I decisively gave up. Then I thought of the QQ Zone API, which can directly obtain QQ avatars and names. I’m sharing it with everyone below.

HTML Code

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>在线获取 QQ 用户名和头像</title>
    </head>
    <body>
        <div> QQ &nbsp;<input type="text" name="" id="qqnum" value="" /></div><br>
        <div> 昵称 <input type="text" name="" id="comname" value="" /></div><br>
        <div> 邮箱 <input type="text" name="" id="commail" value="" /></div><br>
        <div id="avatar"></div>
        <script src="https://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>
        <script>
            $("#qqnum").blur(function() {
                var qq = $("#qqnum").val();
                $("#commail").val(qq + "@qq.com");
                $.ajax({
                    type: "get",
                    url: "https://v1.ikxin.com/api/qqname.php?a=getqqnickname&qq=" + qq,
                    dataType: "jsonp",
                    jsonp: "callback",
                    jsonpCallback: "portraitCallBack",
                    success: function(data) {
                        $("#comname").val(data[qq][6]);
                    },
                    error: function() {
                        $("#comname").val("获取失败");
                    }
                });
                $.ajax({
                    type: "get",
                    url: "https://v1.ikxin.com/api/qqname.php?a=getqqavatar&qq=" + qq,
                    dataType: "jsonp",
                    jsonp: "callback",
                    jsonpCallback: "qqavatarCallBack",
                    success: function(data) {
                        $("#avatar").html("<img src='" + data[qq] + "'>");
                    },
                    error: function() {
                        alert("获取失败");
                    }
                });
            })
        </script>
    </body>
</html>

PHP Code

<?php
header("content-Type: text/html; charset=UTF-8");
$a = @$_GET['a'] ? $_GET['a'] : '';
if(empty($a)){
    header("Location: ../");
    exit;
}
// 获取 QQ 昵称
if($a == "getqqnickname"){
    $qq = isset($_GET['qq']) ? addslashes(trim($_GET['qq'])) : '';
    if(!empty($qq) && is_numeric($qq) && strlen($qq) > 4 && strlen($qq) < 13){
        $qqnickname = file_get_contents('http://users.qzone.qq.com/fcg-bin/cgi_get_portrait.fcg?uins='.$qq);
        if($qqnickname){
            $qqnickname = mb_convert_encoding($qqnickname, "UTF-8", "GBK");
            echo $qqnickname;
        }
    }
}
// 获取 QQ 头像
if($a == "getqqavatar"){
    $qq = isset($_GET['qq']) ? addslashes(trim($_GET['qq'])) : '';
    if(!empty($qq) && is_numeric($qq) && strlen($qq) > 4 && strlen($qq) < 13){
        $qqavatar_1 = file_get_contents('http://ptlogin2.qq.com/getface?appid=52958812&imgtype=4&uin='.$qq);
        $qqavatar = str_replace('http', 'https', $qqavatar_1);
        if($qqavatar){
            echo str_replace('pt.setHeader', 'qqavatarCallBack', $qqavatar);
        }
    }
}

Note

The QQ avatar obtained is in HTTP mode.