tags:

views:

378

answers:

3

Hi,

I'm currently using this C code:

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

It prints the output on the console. How can I get the same output, but read it into, say, a string? (This is a probably a basic question, but I do not yet understand the libcurl API...)

Thanks for any help!

Mike

+4  A: 

You need to pass a function and buffer to write it to buffer.

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

}

Please take a look more info here.

S.Mark
@Mike, Updated with some example, and a link for C and php, basically same concepts.
S.Mark
@S.Mark, cool, nice link!
Mike
+2  A: 

you need a write callback function. I use this kind of function to read the response, error and be able to supply my own headers:

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; 
}
ak
A: 

This can be done using

curl_easy_setopt(easyhandle, CURLOPT_WRITEFUNCTION, write_data);

which sets a callback function write_data which is a function with the signature

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

If you want userp be some internal struct you are using in your program, call

curl_easy_setopt(easyhandle, CURLOPT_WRITEDATA, &internal_struct);

to get the pointer to internal_struct passed to every call of write_data.

fhe