iami233
iami233
文章156
标签37
分类4

文章分类

文章归档

PHP获取QQ昵称和头像API

PHP获取QQ昵称和头像API

返回数据

1
2
3
4
5
6
7
8
9
10
{
"code": 200,
"msg": "success",
"data": {
"qq": "123456",
"name": "腾讯视频",
"img": "https://q1.qlogo.cn/g?b=qq&k=ib0ts0Uexd5Rcic68bVuREwg&kti=Y7bM6AAAAAI&s=140",
"mail": "123456@qq.com"
}
}

接口源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
// title: 获取QQ昵称和头像API
// update: 2024-05-03
// author: iami233
header('Access-Control-Allow-Origin:*');
header('Content-type: application/json');

header('content-type: application/json;charset=utf8');

$qq = $_REQUEST['qq'] ?? null;
if (!$qq || !is_numeric($qq)) {
sendResponse(201, '参数错误');
exit;
}

// 从QQ空间获取 Cookie 即可,分别填入uin、skey、p_skey
$uin = '';
$skey = '';
$p_skey = '';
$cookie = "uin=$uin;skey=$skey;p_skey=$p_skey;";

$url = 'https://r.qzone.qq.com/fcg-bin/cgi_get_portrait.fcg?g_tk=' . getbkn($skey) . '&uins=' . $qq;
$response = getCurl($url, ["Cookie: $cookie"]);

$data = json_decode(substr(mb_convert_encoding($response, "UTF-8", "GBK"), 17, -1), true);

if ($data) {
$server = rand(1, 4);
$txUrl = "https://q$server.qlogo.cn/headimg_dl?dst_uin=$qq&spec=640";
$json = [
'code' => 200,
"msg" => "success",
"data" => [
'qq' => $qq,
'name' => $data[$qq][6],
"img" => $txUrl,
'mail' => $qq.'@qq.com',
]
];
sendResponse(200, 'success', $json['data']);
} else {
sendResponse(201, '接口错误,请联系管理员');
}

function getbkn($skey) {
$hash = 5381;
$len = strlen($skey);
for ($i = 0; $i < $len; $i++) {
$hash += ($hash << 5) + ord($skey[$i]);
}
return $hash & 0x7FFFFFFF;
}

function getCurl($url, $header = []) {
$con = curl_init($url);
curl_setopt_array($con, [
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_AUTOREFERER => true,
CURLOPT_HTTPHEADER => $header,
CURLOPT_TIMEOUT => 5000
]);
$result = curl_exec($con);
curl_close($con);
return $result;
}

function sendResponse($code, $msg, $data = null) {
$response = [
'code' => $code,
'msg' => $msg,
'data' => $data
];
echo json_encode($response, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE);
exit;
}
本文作者:iami233
本文链接:https://5ime.cn/477.html
版权声明:本文采用 CC BY-NC-SA 3.0 CN 协议进行许可