cURL - 将输出放入变量?

时间:2010-04-05 09:17:51

标签: c libcurl

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

感谢您的帮助!

麦克

5 个答案:

答案 0 :(得分: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));

}

请查看更多信息here

答案 1 :(得分: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; 
}

答案 2 :(得分: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的每次调用。

答案 3 :(得分: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();

}

答案 4 :(得分: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);
}