根据QQ账号获取QQ头像和名称PHP源码,无乱码、无需Cookie
<?php
/*
*/
error_reporting(0);
header('Access-Control-Allow-Origin:*');
header('Content-Type:application/json;charset=utf-8');
$qq = $_REQUEST['qq'];
if(empty($qq)){
exit(json_encode(['code'=> -1,'msg'=>'参数错误!'],448));
}
$qq_info = get_qq_nick($qq);
if(empty($qq_info['img'])){
exit(json_encode(['code'=> -1,'msg'=>'获取失败,请检查qq号是否正确!'],448));
}
$return_data = [
'code' => 200,
'msg' => '获取成功',
'data' => $qq_info
];
exit(json_encode($return_data,448));
function get_qq_nick($qq) {
$curl = curl_init();
$ip = rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255);
curl_setopt_array($curl, [
CURLOPT_URL => 'https://users.qzone.qq.com/fcg-bin/cgi_get_portrait.fcg?uins='.$qq,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_POSTFIELDS => '------WebKitFormBoundaryYTwvlk5brGmyD3Mn',
CURLOPT_HTTPHEADER => [
'Content-Type: multipart/form-data; boundary=---011000010111000001101001',
'X-FORWARDED-FOR:'.$ip,
'CLIENT-IP:'.$ip
],
]);
$response = curl_exec($curl);
$encode = mb_detect_encoding($response, array("ASCII",'UTF-8',"GB2312","GBK",'BIG5'));
$response = mb_convert_encoding($response, 'UTF-8', $encode);
$data = json_decode(substr($response, 17, -1), true);
$Array = [
'img' => str_replace('http','https://',$data[$qq][0]),
'name' => $data[$qq][6]
];
return $Array;
}