随机获取网易云音乐热门歌曲接口
接口地址:/api/rand.music.163.php
返回格式:get/post
请求方式:json/mp3
请求示例:/api/rand.music.163.php?format=json
请求参数说明:
返回参数说明:
请求返回示例:
{"code": 200,"name": "凉城","artists_name": "任然","music_url": "http:\/\/\/song\/media\/outer\/url?id=442314991.mp3","music_pic": "http:\/\/p1.\/9RQepityGQUfi5Rbcz7xCQ==\/18747772766555079.jpg"}
#include <iostream>#include <curl/curl.h>#include <json/json.h>#include <String>#pragma warning(disable:4996)using namespace std;wchar_t *utf_8ToUnicode(const char *u8s){int wcsLen = MultiByteToWideChar(CP_UTF8, NULL, u8s, strlen(u8s), NULL, NULL);wchar_t *wcString = new wchar_t[wcsLen + 1];MultiByteToWideChar(CP_UTF8, NULL, u8s, strlen(u8s), wcString, wcsLen);wcString[wcsLen] = '\0';return wcString;}char *unicodeToAnsi(wchar_t *wcString){int len = WideCharToMultiByte(CP_ACP, NULL, wcString, -1, NULL, NULL, NULL, NULL);char *str = new char[len];WideCharToMultiByte(CP_ACP, NULL, wcString, -1, str, len, NULL, NULL);return str;}static void UTF8toANSI(char *strUTF8, char*out_ansi){//获取转换为多字节后需要的缓冲区大小,创建多字节缓冲区UINT nLen = MultiByteToWideChar(CP_UTF8, 0, strUTF8, -1, NULL, 0);LPWSTR wszBuffer = (LPWSTR)malloc((nLen + 1) * sizeof(WCHAR));nLen = MultiByteToWideChar(CP_UTF8, 0, strUTF8, -1, wszBuffer, nLen);wszBuffer[nLen] = 0;nLen = WideCharToMultiByte(936, 0, wszBuffer, -1, NULL, 0, NULL, NULL);nLen = WideCharToMultiByte(936, 0, wszBuffer, -1, out_ansi, nLen, NULL, NULL);out_ansi[nLen] = 0;}wstring AsciiToUnicode(const string& str){// 预算-缓冲区中宽字节的长度 int unicodeLen = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, nullptr, 0);// 给指向缓冲区的指针变量分配内存 wchar_t *pUnicode = (wchar_t*)malloc(sizeof(wchar_t)*unicodeLen);// 开始向缓冲区转换字节 MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, pUnicode, unicodeLen);wstring ret_str = pUnicode;free(pUnicode);return ret_str;}string UnicodeToUtf8(const wstring& wstr){// 预算-缓冲区中多字节的长度 int ansiiLen = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr);// 给指向缓冲区的指针变量分配内存 char *pAssii = (char*)malloc(sizeof(char)*ansiiLen);// 开始向缓冲区转换字节 WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, pAssii, ansiiLen, nullptr, nullptr);string ret_str = pAssii;free(pAssii);return ret_str;}string AsciiToUtf8(const string& str){return UnicodeToUtf8(AsciiToUnicode(str));}/* 不考虑任何异常的情况, 简单的get请求 *//**** buffer 接收数据所在的缓冲区* size要读取的字节数* count读写size长度的数据count次* response用户自定义文件指针*/size_t getUrlResponse(char* buffer, size_t size, size_t count, string* response) {size_t recv_size = size * count;response->clear();response->append(buffer);return recv_size;}string get(const string& url) {// 请求数据string response;// easy handle声明CURL *handle;// 初始化handlehandle = curl_easy_init();// 设置urlcurl_easy_setopt(handle, CURLOPT_URL, url.c_str());//curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "POST");//设置curl的请求头// struct curl_slist* header_list = NULL;// header_list = curl_slist_append(header_list, "Content-Type:text/html;charset=UTF-8");//Content-Type:application/x-www-form-urlencoded Content-Type:application/json// curl_easy_setopt(handle, CURLOPT_HTTPHEADER, header_list);//curl_easy_setopt(handle, CURLOPT_HTTPGET, NULL);//自定义请求方式curl_easy_setopt(handle, CURLOPT_POST, 1);//设置为非0表示本次操作为POST//设置ssl验证curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, false);curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, false);string data2,data ="format=json";data2 = AsciiToUtf8(data.c_str());//设置post请求的参数curl_easy_setopt(handle, CURLOPT_POSTFIELDS, data);// 注册回调函数curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, &getUrlResponse);// 获取信息curl_easy_setopt(handle, CURLOPT_WRITEDATA, &response);// 执行请求curl_easy_perform(handle);// 释放curl_easy_cleanup(handle);return response;}int main() {// 使用前初始化libcurl, 只需初始化一次curl_global_init(CURL_GLOBAL_DEFAULT);string _stRet = get("/api/rand.music.163.php");// 执行请求cout << _stRet;Json::Reader reader;Json::Value root;if (!reader.parse(_stRet, root, false)){cout << "reader parse error: " << strerror(errno) << endl;return -1;}string msg;int code;int size;size = root.size();cout << "total " << size << " elements" << endl;code = root["code"].asInt();msg = root["msg"].asString(); //cout << "msg: " << unicodeToAnsi(utf_8ToUnicode(msg.c_str())) << ", code: " << code << endl;cout << " code: " << code << endl;cout << " name: " << unicodeToAnsi(utf_8ToUnicode(root["name"].asString().c_str())) << endl;cout << " artists_name: " << unicodeToAnsi(utf_8ToUnicode(root["artists_name"].asString().c_str())) << endl;cout << " music_url: " << unicodeToAnsi(utf_8ToUnicode(root["music_url"].asString().c_str())) << endl;cout << " music_pic: " << unicodeToAnsi(utf_8ToUnicode(root["music_pic"].asString().c_str())) << endl;// 释放libcurl相关资源curl_global_cleanup();getchar();return 0;}
方法2:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <curl/curl.h>#pragma warning(disable:4996)using namespace std;#define POSTURL "/api/rand.music.163.php"#define POSTFIELDS "format=json"#define FILENAME "curlposttest.log"size_t write_data(void* buffer, size_t size, size_t nmemb, void *stream){FILE *fptr = (FILE*)stream;fwrite(buffer, size, nmemb, fptr);return size * nmemb;}int main(int argc, char *argv[]){CURL *curl;CURLcode res;FILE* fptr;struct curl_slist *http_header = NULL;if ((fptr = fopen(FILENAME, "w")) == NULL){fprintf(stderr, "fopen file error:%s\n", FILENAME);return -1;}curl = curl_easy_init();if (!curl){fprintf(stderr, "curl init failed\n");return -1;}//设置curl的请求头// struct curl_slist* header_list = NULL;// header_list = curl_slist_append(header_list, "Content-Type:text/html;charset=UTF-8");//Content-Type:application/x-www-form-urlencoded Content-Type:application/json// curl_easy_setopt(handle, CURLOPT_HTTPHEADER, header_list);//设置ssl验证curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);curl_easy_setopt(curl, CURLOPT_URL, POSTURL); //url地址curl_easy_setopt(curl, CURLOPT_POSTFIELDS, POSTFIELDS); //post参数curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); //对返回的数据进行操作的函数地址curl_easy_setopt(curl, CURLOPT_WRITEDATA, fptr); //这是write_data的第四个参数值curl_easy_setopt(curl, CURLOPT_POST, 1); //设置问非0表示本次操作为postcurl_easy_setopt(curl, CURLOPT_VERBOSE, 1); //打印调试信息curl_easy_setopt(curl, CURLOPT_HEADER, 1); //将响应头信息和相应体一起传给write_datacurl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); //设置为非0,响应头信息location//curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "/Users/zhu/CProjects/curlposttest.cookie");res = curl_easy_perform(curl);if (res != CURLE_OK){switch (res){case CURLE_UNSUPPORTED_PROTOCOL:fprintf(stderr, "不支持的协议,由URL的头部指定\n");case CURLE_COULDNT_CONNECT:fprintf(stderr, "不能连接到remote主机或者代理\n");case CURLE_HTTP_RETURNED_ERROR:fprintf(stderr, "http返回错误\n");case CURLE_READ_ERROR:fprintf(stderr, "读本地文件错误\n");default:fprintf(stderr, "返回值:%d\n", res);}return -1;}curl_easy_cleanup(curl);}
案例下载:/download/greless/18434548