14

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

谢谢你的帮助!

麦克风

4

5 回答 5

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));

}

请在此处查看更多信息。

于 2010-04-05T09:22:33.753 回答
4

你需要一个写回调函数。我使用这种函数来读取响应、错误并能够提供我自己的标题:

size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
    std::string buf = std::string(static_cast<char *>(ptr), size * nmemb);
    std::stringstream *response = static_cast<std::stringstream *>(stream);
    response->write(buf.c_str(), (std::streamsize)buf.size());
    return size * nmemb;
}

bool CurlGet(
    const std::string &url, 
    const std::vector<std::string> &headers, 
    std::stringstream &response, 
    std::string &error)
{

    curl_global_init(CURL_GLOBAL_ALL);

    curl_slist *headerlist = NULL;

    std::vector<std::string>::const_iterator it;
    for (it = headers.begin(); it < headers.end(); it++) {
        headerlist = curl_slist_append(headerlist, it->c_str());
    }   

    CURL *curl = curl_easy_init();
    char ebuf[CURL_ERROR_SIZE];
    curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
    curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, ebuf);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
    CURLcode res = curl_easy_perform(curl); 
    curl_easy_cleanup(curl);
    curl_slist_free_all(headerlist);

    if (res != CURLE_OK)
        error = ebuf;
    else
        error.clear();

    return res == CURLE_OK; 
}
于 2010-04-05T09:26:53.977 回答
2

这可以使用

curl_easy_setopt(easyhandle, CURLOPT_WRITEFUNCTION, write_data);

它设置了一个回调函数write_data,它是一个带有签名的函数

size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp);

如果您想userp成为您在程序中使用的一些内部结构,请调用

curl_easy_setopt(easyhandle, CURLOPT_WRITEDATA, &internal_struct);

获取internal_struct传递给每个调用的指针write_data

于 2010-04-05T09:28:53.570 回答
1

嗨,我解决了从回调函数返回代码 23 以从回调函数返回大小的问题。

见下面的代码:

/* 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));

    return ((size_t)(size *nmemb));
//if you not send return value of size it will show you ERROR CODE 23return  curl_easy_perform();

}
于 2015-03-27T09:21:54.127 回答
0

其他示例均不适合我。
这是我最终做的事情:

size_t static curl_write(void *buffer, size_t size, size_t nmemb, void *userp)
{
     userp += strlen(userp);  // Skipping to first unpopulated char
     memcpy(userp, buffer, nmemb);  // Populating it.
     return nmemb;
}

int GetCurl()
{
     CURL *curl;
     CURLcode res;

     char *s = (char *) malloc(512);

     curl = curl_easy_init();
     if (curl)
     {
          curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com");
          curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write);
          curl_easy_setopt(curl, CURLOPT_WRITEDATA, s);
          res = curl_easy_perform(curl);
          curl_easy_cleanup(curl);
     }

     printf("GREAT SUCCESS!! Your string is %s\n", s);
}
于 2016-11-03T12:56:58.140 回答