Curl:此curl语句中涉及哪些API调用?

时间:2014-08-28 02:17:10

标签: c curl

执行此curl命令时,我正试图弄清楚在curl API调用方面“幕后”发生了什么:

curl "http://someURL" --header "apikey:someNumbers" --header "Content-Type:audio/x-wav"
    --header "lngCode:en_US" --data-binary @audiofile.wav

换句话说:如何使用curl API在C中执行上述操作?

除了将此二进制文件发布到远程服务器之外,我还对如何使用curl解析来自服务器的响应感兴趣(服务器分析音频文件并将一些结果返回给客户端)。

1 个答案:

答案 0 :(得分:1)

命令:

curl "http://someURL" --header "apikey:someNumbers" --header "Content-Type:audio/x-wav" --header "lngCode:en_US" --data-binary @audiofile.wav

粗略地转换为以下libcurl函数调用:

curl_global_init(CURL_GLOBAL_DEFAULT);

CURL *curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, "http://someURL");

curl_slist *headers = curl_slist_append(NULL, "apikey:someNumbers");
curl_slist_append(headers, "Content-Type:audio/x-wav");
curl_slist_append(headers, "lngCode:en_US");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

// read content of "audiofile.wav" into a memory buffer, then...
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, (char*) <pointer to memory buffer>);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, (curl_off_t) <size of memory buffer>);

curl_easy_perform(curl);

curl_easy_cleanup(curl);
curl_slist_free_all(headers);

curl_global_cleanup();