gpt4 book ai didi

cURL - 将输出放入变量?

转载 作者:太空狗 更新时间:2023-10-29 16:35:25 25 4
gpt4 key购买 nike

我目前正在使用这个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com