I read the man page, but i cant make it work properly... Please correct me if im wrong. If i want to get the POST answer in a variable (char *) but the options CURLOPT_WRITEFUNCTION and CURLOPT_WRITEDATA are said to write in files...
I also dont know which one i have to use, or if i have to use both. According to the manual:
"If you're using libcurl as a win32 DLL, you MUST use the CURLOPT_WRITEFUNCTION if you set this option (CURLOPT_WRITEDATA) or you will experience crashes."
This is the example i made using the options, based on some examples. Is it correct? How can i get the info i need?
Code:
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
struct MemoryStruct {
char *memory;
size_t size;
};
static void *myrealloc(void *ptr, size_t size) {
/* There might be a realloc() out there that doesn't like reallocing
NULL pointers, so we take care of it here */
if(ptr)
return realloc(ptr, size);
else
return malloc(size);
}
static size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data) {
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)data;
mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize + 1);
if (mem->memory) {
memcpy(&(mem->memory[mem->size]), ptr, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
}
return realsize;
}
int main(void){
CURL *curl;
CURLcode res;
struct MemoryStruct chunk;
chunk.memory=NULL;
chunk.size = 0;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "www.xxx.zzz.php");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "in_chkcacic=chkinfo");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
return 0;
}