基于网易云音乐API的在线音乐搜索平台
以下是各编程语言调用本API的示例(接口地址:当前域名/api.php),支持通过关键词搜索或歌曲ID查询
.版本 2
.支持库 internet
.子程序 搜索歌曲, 文本型, , 返回JSON结果
局部变量 关键词, 文本型
局部变量 接口地址, 文本型
局部变量 编码关键词, 文本型
关键词 = "周杰伦" ' 搜索关键词
编码关键词 = 编码_URL编码 (关键词, 真) ' 处理中文编码
接口地址 = "api.php?gq=" + 编码关键词
' 发送请求并返回结果
返回 (网页_访问 (接口地址, , , , , "User-Agent: Mozilla/5.0"))
.子程序 获取单首歌曲, 文本型
局部变量 歌曲ID, 文本型
局部变量 接口地址, 文本型
歌曲ID = "123456" ' 歌曲ID
接口地址 = "api.php?id=" + 歌曲ID
返回 (网页_访问 (接口地址, , , , , "User-Agent: Mozilla/5.0"))
// 搜索歌曲
async function searchSongs(keyword) {
try {
const url = `api.php?gq=${encodeURIComponent(keyword)}`;
const response = await fetch(url);
const data = await response.json();
console.log('搜索结果:', data);
return data;
} catch (error) {
console.error('请求失败:', error);
}
}
// 获取单首歌曲(通过ID)
async function getSongById(id) {
try {
const url = `api.php?id=${id}`;
const response = await fetch(url);
const data = await response.json();
console.log('歌曲详情:', data);
return data;
} catch (error) {
console.error('请求失败:', error);
}
}
// 调用示例
searchSongs('周杰伦');
getSongById('123456');
import requests
import urllib.parse
def search_songs(keyword):
"""通过关键词搜索歌曲"""
encoded_keyword = urllib.parse.quote(keyword)
url = f"api.php?gq={encoded_keyword}"
try:
response = requests.get(url)
data = response.json()
print("搜索结果:", data)
return data
except Exception as e:
print("请求失败:", e)
def get_song_by_id(song_id):
"""通过ID获取单首歌曲"""
url = f"api.php?id={song_id}"
try:
response = requests.get(url)
data = response.json()
print("歌曲详情:", data)
return data
except Exception as e:
print("请求失败:", e)
# 调用示例
search_songs("周杰伦")
get_song_by_id("123456")
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
public class MusicApiExample {
// 基础API地址
private static final String API_BASE = "api.php";
// 搜索歌曲
public static void searchSongs(String keyword) throws Exception {
String encodedKeyword = URLEncoder.encode(keyword, StandardCharsets.UTF_8.name());
String url = API_BASE + "?gq=" + encodedKeyword;
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.build();
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(response -> System.out.println("搜索结果:" + response))
.join();
}
// 通过ID获取歌曲
public static void getSongById(String id) throws Exception {
String url = API_BASE + "?id=" + id;
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.build();
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(response -> System.out.println("歌曲详情:" + response))
.join();
}
public static void main(String[] args) throws Exception {
searchSongs("周杰伦");
getSongById("123456");
}
}
using System;
using System.Net.Http;
using System.Threading.Tasks;
class MusicApiClient {
private static readonly string ApiBase = "api.php";
// 搜索歌曲
public static async Task SearchSongs(string keyword) {
string encodedKeyword = Uri.EscapeDataString(keyword);
string url = $"{ApiBase}?gq={encodedKeyword}";
using (HttpClient client = new HttpClient()) {
try {
string response = await client.GetStringAsync(url);
Console.WriteLine("搜索结果:" + response);
} catch (Exception ex) {
Console.WriteLine("请求失败:" + ex.Message);
}
}
}
// 通过ID获取歌曲
public static async Task GetSongById(string id) {
string url = $"{ApiBase}?id={id}";
using (HttpClient client = new HttpClient()) {
try {
string response = await client.GetStringAsync(url);
Console.WriteLine("歌曲详情:" + response);
} catch (Exception ex) {
Console.WriteLine("请求失败:" + ex.Message);
}
}
}
static async Task Main(string[] args) {
await SearchSongs("周杰伦");
await GetSongById("123456");
}
}