我目前正在使用这个 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);
}
它在控制台上打印输出。我怎样才能得到相同的输出,但将它读入,比如说,一个字符串? (这可能是一个基本问题,但我还不了解 libcurl API...)
感谢您的帮助!
迈克
最佳答案
您需要传递一个函数和缓冲区以将其写入缓冲区。
/* 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));
}
请查看更多信息here .
关于cURL - 将输出放入变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2577654/