我目前正在使用此C代码:
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://my-domain.org/");
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
Run Code Online (Sandbox Code Playgroud)
它在控制台上打印输出.我怎样才能得到相同的输出,但是把它读成一个字符串?(这可能是一个基本问题,但我还不了解libcurl API ......)
谢谢你的帮助!
麦克风
YOU*_*YOU 16
您需要传递一个函数和缓冲区以将其写入缓冲区.
/* setting a callback function to return the data */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_callback_func);
/* passing the pointer to the response as the callback parameter */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &response);
/* the function to invoke as the data recieved */
size_t static write_callback_func(void *buffer,
size_t size,
size_t nmemb,
void *userp)
{
char **response_ptr = (char**)userp;
/* assuming the response is a string */
*response_ptr = strndup(buffer, (size_t)(size *nmemb));
}
Run Code Online (Sandbox Code Playgroud)
请在这里查看更多信息.