简体   繁体   中英

C++ curl callback

Can anyone inform me why we need a callback in some curl options, like CURLOPT_WRITEFUNCTION ?

I used the code below in C++, and got the same result with or without a CURLOPT_WRITEFUNCTION callback, so it seems confusing to use a callback.

int main(){
    CURL *curl = curl_easy_init();
    CURLcode response;
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com");
        response=curl_easy_perform(curl);
        curl_easy_cleanup(curl);
    }    
}

callback is an option that has it own specific properties that might be usefull for someone who wants to use that properties. see link below https://curl.se/libcurl/c/CURLOPT_WRITEFUNCTION.html

By default, libcurl simply writes the downloaded data to STDOUT.

If you just want to change which FILE* it writes the data to, you can use the CURLOPT_WRITEDATA option.

But, if you want to change how it writes the data, for instance to write to something other than a FILE* , you can use a CURLOPT_WRITEFUNCTION callback.

For instance, see these examples, which use CURLOPT_WRITEFUNCTION to customize downloads:

https://curl.se/libcurl/c/ftpsget.html

https://curl.se/libcurl/c/getinmemory.html

https://curl.se/libcurl/c/http2-pushinmemory.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM