目前全网的QQ昵称获取接口均已失效,能用的接口则需要用到QQ的Cookie,调用难度较大,现在就给大家分享一个可以直接使用的接口。
教书先生API:https://api.oioweb.cn
此接口网站不需要登录,可以直接调用,2018年运行至今已经算是非常的稳定了 ,可以放心使用。
演示接口地址:https://api.oioweb.cn/api/qq/info?qq=599928887
调用方法
PHP (使用 cURL)
GET请求方式:
<?php
$qq_number = '599928887';
$url = "https://api.oioweb.cn/api/qq/info?qq={$qq_number}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if(curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
} else {
// 解析并输出响应结果
$responseData = json_decode($response, true);
print_r($responseData);
}
curl_close($ch);
?>
POST请求方式:
<?php
$qq_number = '599928887';
$url = "https://api.oioweb.cn/api/qq/info";
$data = array('qq' => $qq_number);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if(curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
} else {
// 解析并输出响应结果
$responseData = json_decode($response, true);
print_r($responseData);
}
curl_close($ch);
?>
JavaScript (使用 fetch API)
GET请求方式:
const qqNumber = '599928887';
const url = `https://api.oioweb.cn/api/qq/info?qq=${qqNumber}`;
fetch(url)
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
POST请求方式:
const qqNumber = '599928887';
const url = 'https://api.oioweb.cn/api/qq/info';
fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: `qq=${encodeURIComponent(qqNumber)}`
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
Node.js (使用 axios)
GET请求方式:
const axios = require('axios');
const qqNumber = '599928887';
const url = `https://api.oioweb.cn/api/qq/info?qq=${qqNumber}`;
axios.get(url)
.then(response => console.log(response.data))
.catch(error => console.error(error));
POST请求方式:
const axios = require('axios');
const qqNumber = '599928887';
const url = 'https://api.oioweb.cn/api/qq/info';
axios.post(url, { qq: qqNumber })
.then(response => console.log(response.data))
.catch(error => console.error(error));
Go (使用 net/http 库)
GET请求方式:
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
func main() {
qqNumber := "599928887"
url := fmt.Sprintf("https://api.oioweb.cn/api/qq/info?qq=%s", qqNumber)
resp, err := http.Get(url)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
var data map[string]interface{}
json.Unmarshal([]byte(body), &data)
fmt.Println(data)
}
POST请求方式:
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
func main() {
qqNumber := "599928887"
url := "https://api.oioweb.cn/api/qq/info"
payload := []byte(`{"qq": "` + qqNumber + `"}`)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(payload))
if err != nil {
fmt.Println(err)
return
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
var result map[string]interface{}
json.Unmarshal(body, &result)
fmt.Println(result)
}
Python (使用 requests 库)
GET请求方式:
import requests
qq_number = '599928887'
url = f"https://api.oioweb.cn/api/qq/info?qq={qq_number}"
response = requests.get(url)
data = response.json()
print(data)
POST请求方式:
import requests
qq_number = '599928887'
url = 'https://api.oioweb.cn/api/qq/info'
payload = {'qq': qq_number}
response = requests.post(url, data=payload)
data = response.json()
print(data)
Java (使用 HttpClient)
GET请求方式:
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
public class Main {
public static void main(String[] args) throws Exception {
String qqNumber = "599928887";
String url = "https://api.oioweb.cn/api/qq/info?qq=" + qqNumber;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet request = new HttpGet(url);
HttpResponse response = httpClient.execute(request);
if (response.getStatusLine().getStatusCode() == 200) {
String responseBody = EntityUtils.toString(response.getEntity());
JSONObject jsonObject = new JSONObject(responseBody);
System.out.println(jsonObject.toString(2));
}
httpClient.close();
}
}
POST请求方式:
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
public class Main {
public static void main(String[] args) throws Exception {
String qqNumber = "599928887";
String url = "https://api.oioweb.cn/api/qq/info";
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost postRequest = new HttpPost(url);
StringEntity input = new StringEntity("{\"qq\":\"" + qqNumber + "\"}");
input.setContentType("application/json");
postRequest.setEntity(input);
CloseableHttpResponse response = httpClient.execute(postRequest);
if (response.getStatusLine().getStatusCode() == 200) {
String responseBody = EntityUtils.toString(response.getEntity());
JSONObject jsonObject = new JSONObject(responseBody);
System.out.println(jsonObject.toString(2));
}
httpClient.close();
}
}
请注意,在实际应用中,请根据实际情况处理错误和异常,以及可能需要的额外头部信息如认证等。同时,上述示例假定API返回的是JSON格式的数据,因此进行了相应的解析操作。如果API的实际行为与这些假设不符,请相应地调整代码。