C libcurl get output into a string

前端 未结 4 1150
粉色の甜心
粉色の甜心 2020-11-28 18:36

I want to store the result of this curl function in a variable, how can I do so?

#include 
#include 

int main(void)
{
  CU         


        
4条回答
  •  情歌与酒
    2020-11-28 19:06

    You can set a callback function to receive incoming data chunks using curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, myfunc);

    The callback will take a user defined argument that you can set using curl_easy_setopt(curl, CURLOPT_WRITEDATA, p)

    Here's a snippet of code that passes a buffer struct string {*ptr; len} to the callback function and grows that buffer on each call using realloc().

    #include 
    #include 
    #include 
    #include 
    
    struct string {
      char *ptr;
      size_t len;
    };
    
    void init_string(struct string *s) {
      s->len = 0;
      s->ptr = malloc(s->len+1);
      if (s->ptr == NULL) {
        fprintf(stderr, "malloc() failed\n");
        exit(EXIT_FAILURE);
      }
      s->ptr[0] = '\0';
    }
    
    size_t writefunc(void *ptr, size_t size, size_t nmemb, struct string *s)
    {
      size_t new_len = s->len + size*nmemb;
      s->ptr = realloc(s->ptr, new_len+1);
      if (s->ptr == NULL) {
        fprintf(stderr, "realloc() failed\n");
        exit(EXIT_FAILURE);
      }
      memcpy(s->ptr+s->len, ptr, size*nmemb);
      s->ptr[new_len] = '\0';
      s->len = new_len;
    
      return size*nmemb;
    }
    
    int main(void)
    {
      CURL *curl;
      CURLcode res;
    
      curl = curl_easy_init();
      if(curl) {
        struct string s;
        init_string(&s);
    
        curl_easy_setopt(curl, CURLOPT_URL, "curl.haxx.se");
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
        res = curl_easy_perform(curl);
    
        printf("%s\n", s.ptr);
        free(s.ptr);
    
        /* always cleanup */
        curl_easy_cleanup(curl);
      }
      return 0;
    }
    

提交回复
热议问题