curl_easy_setopt (CURLOPT_WRITEFUNCTION) of libcurl callback function to set the write function

When set, if the data obtained for curl call the callback processing, then the following two settings

1、curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
2、curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);    

Detailed below
.

A, CURLOPT_WRITEFUNCTION

#include <curl/curl.h>
 
size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata);
 
CURLcode curl_easy_setopt(CURL *handle, CURLOPT_WRITEFUNCTION, write_callback);

In CURLOPT_WRITEFUNCTION setting properties, the use of callback processing write_callback

Upon receipt of the data needs to be saved, libcurl will call this callback function.
for most transmission, the callback is called multiple times, each call will be another piece of data transmitted.
* default output data is output to the standard case, fwrite default callback

ptr points to data transfer, the data size is nmemb; size is always 1.

Official websitehttps://curl.haxx.se/libcurl/c/CURLOPT_WRITEFUNCTION.html

.

Two, CURLOPT_WRITEDATA

#include <curl/curl.h>
CURLcode curl_easy_setopt(CURL *handle, CURLOPT_WRITEDATA, void *pointer);

Under this option, the data will be transmitted pointer passed to the callback function to write,

If you use CURLOPT_WRITEFUNCTION option, the void * pointer will be passed to the callback fourth parameter void * userdata

If you do not use CURLOPT_WRITEFUNCTION option, you must be here if the file pointer of type 'FILE *' (cast to 'void *'), used to transmit standard output by default, that is, fwrite (3)
( By default, this is a FILE * to stdout.)

Official websitehttps://curl.haxx.se/libcurl/c/CURLOPT_WRITEDATA.html

.

Third, the official website example

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

//getinmemory.c

 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#include <curl/curl.h>
 
struct MemoryStruct {
  char *memory;
  size_t size;
};
 
static size_t
WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
  size_t realsize = size * nmemb;
  struct MemoryStruct *mem = (struct MemoryStruct *)userp;
 
 // Note that dynamic allocation size of the buffer again according to data obtained each time you call
  char *ptr = realloc(mem->memory, mem->size + realsize + 1); 
  if(ptr == NULL) {
    /* out of memory! */ 
    printf("not enough memory (realloc returned NULL)\n");
    return 0;
  }
 
  mem->memory = ptr;
  memcpy(&(mem->memory[mem->size]), contents, realsize);
  mem->size += realsize;
  mem->memory[mem->size] = 0;
 
  return realsize;
}
 
int main(void)
{
  CURL *curl_handle;
  CURLcode res;
 
  struct MemoryStruct chunk;
 
  chunk.memory = malloc(1);  /* will be grown as needed by the realloc above */ 
  chunk.size = 0;    /* no data at this point */ 
 
  curl_global_init(CURL_GLOBAL_ALL);
 
  /* init the curl session */ 
  curl_handle = curl_easy_init();
 
  /* specify URL to get */ 
  curl_easy_setopt(curl_handle, CURLOPT_URL, "https://www.example.com/");
 
  /* send all data to this function  */ 
     // For the same blocking curl_easy_perform, the data obtained before the finish, called many times WriteMemoryCallback
  curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
  
 
  /* we pass our 'chunk' struct to the callback function */ 
  curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
 
  /* some servers don't like requests that are made without a user-agent   field, so we provide one */ 
  curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
 
  /* get it! */ 
     // For the same blocking curl_easy_perform, the data obtained before the finish, called many times WriteMemoryCallback
  res = curl_easy_perform(curl_handle);
 
  /* check for errors */ 
  if(res != CURLE_OK) {
    fprintf(stderr, "curl_easy_perform() failed: %s\n",
            curl_easy_strerror(res));
  }
  else {
    /*
     * Now, our chunk.memory points to a memory block that is chunk.size
     * bytes big and contains the remote file.
     *
     * Do something nice with it!
     */ 
 
    printf("%lu bytes retrieved\n", (unsigned long)chunk.size);
  }
 
  /* cleanup curl stuff */ 
  curl_easy_cleanup(curl_handle);
 
  free(chunk.memory);
 
  /* we're done with libcurl, so clean it up */ 
  curl_global_cleanup();
 
  return 0;
}

Intelligent Recommendation

A simple way to write a callback function

Tutural repeatability In the previous article, we discussed this topic: If the callback function is a class's member function, this callback function must be declared static. Write a static callback f...

Simply write a backup and callback function

Simply write a backup and callback function Backup Callback Backup Statistical Backup Save Path Number, Excess Backup Number Removal, Python3 File, Name: Sort_min.py Callback...

LibCURL Getting Started Related Interface Functions CURL_EASY_SETOPT

Namecurl_easy_setopt prototype #include <curl/curl.h> CURLcode curl_easy_setopt(CURL *handle, CURLoption option, parameter); describe CURL_EASY_SETOPT is used to tell LibCurl what to do, or tell...

wepy there are two different ways to write the callback function

When reference (NB: only these two code snippets, look like with format)   ...

More Recommendation

Two ways to write js callback function

Callback Applications often call pre-prepared functions in the library through APIs. But some library functions require the application to pass a function to it first, so that it can be called at the ...

How to write a callback function of C language

Why can't 80% of the code farmers can't do architects? >>>   The callback function is often said.callback, Using the C language development process, use itCallbackIt can develop hig...

Tensorflow-KERAS 5. Write a callback function

I. Overview of the callback function 1. The function of the callback function The callback is a powerful tool that customizes the behavior of the KERAS model during training, evaluation or reasoning. ...

Ajax's callback function (JS and jQuery Write)

What is a Ajax callback function If you want to process the data obtained after the AJAX request, you need to use a callback function. success:Successful call after request, incoming data error:Post-r...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top