libcurl problem with getting data from curl_easy_perform()
To get the data into string, you need to set up a write callback function:
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback_func);
Also, the address of your string variable to receive the data:
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &str)
Callback function would look like this:
size_t callback_func(void *ptr, size_t size, size_t count, void *stream)
{
/* ptr - your string variable.
stream - data chuck you received */
printf("%.*s", size, (char*)stream);
}
Because you won't know the total size of data you'd be receiving so you would need to make pointer re-allocations to get it into a string.
The other answer appears to be wrong in its usage of first and last parameters of callback_func
(see the docs). Actual chunk of data you received is in the first parameter, ptr
, while the pointer you pass with CURLOPT_WRITEDATA
is the last parameter.
I've made a complete compilable example:
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
size_t dataSize=0;
size_t curlWriteFunction(void* ptr, size_t size/*always==1*/,
size_t nmemb, void* userdata)
{
char** stringToWrite=(char**)userdata;
const char* input=(const char*)ptr;
if(nmemb==0) return 0;
if(!*stringToWrite)
*stringToWrite=malloc(nmemb+1);
else
*stringToWrite=realloc(*stringToWrite, dataSize+nmemb+1);
memcpy(*stringToWrite+dataSize, input, nmemb);
dataSize+=nmemb;
(*stringToWrite)[dataSize]='\0';
return nmemb;
}
int main()
{
char* data=0;
CURL*const curl=curl_easy_init();
if(!curl)
{
fprintf(stderr, "Failed to init curl");
return 1;
}
curl_easy_setopt(curl, CURLOPT_URL, "https://www.google.com");
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &curlWriteFunction);
if(curl_easy_perform(curl)!=CURLE_OK)
{
fprintf(stderr, "Failed to get web page\n");
return 1;
}
curl_easy_cleanup(curl);
if(!data)
{
fprintf(stderr, "Got no data\n");
return 1;
}
printf("Page data:\n\n%s\n", data);
free(data);
}