Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone explain the arguments to write function used for the curl option CURLOPT_WRITEFUNCTION

Tags:

curl

From http://curl.haxx.se/libcurl/c/libcurl-tutorial.html:

So, you write your own function that matches this prototype:

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

You tell libcurl to pass all data to this function by issuing a function similar to this:

 curl_easy_setopt(easyhandle, CURLOPT_WRITEFUNCTION, write_data);

Can someone explain what the arguments size and nmemb stand for? Is size the number of characters in the response? Then what is nmemb?

like image 321
Hugh Darling Avatar asked Apr 18 '11 10:04

Hugh Darling


2 Answers

This is designed for the function such as: size_t fwrite(const void* buffer, size_t size, size_t count, FILE* stream);

FILE* out = fopen("out.html", "w");
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)out);
curl_easy_setopt(easyhandle, CURLOPT_WRITEFUNCTION, fwrite);

So we need not to add more code for fwrite as it is!

like image 97
SmartXiaoMing Avatar answered Oct 25 '22 22:10

SmartXiaoMing


Apparently "size is the size of one data item, nmemb is the number of data items". My guess is that it's some internal implementation detail. The real data size = size * nmemb.

The documentation for the callback function: https://curl.haxx.se/libcurl/c/CURLOPT_WRITEFUNCTION.html

Example code:
https://github.com/curl/curl/blob/master/docs/examples/getinmemory.c

like image 9
Peter Tseng Avatar answered Oct 25 '22 21:10

Peter Tseng