且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ç的libcurl获取输出到一个字符串

更新时间:2023-02-04 23:36:34

您可以设置一个回调函数中使用 curl_easy_setopt(卷曲,CURLOPT_WRITEFUNCTION,MYFUNC)接收传入的数据块;

回调将带您可以通过设置用户定义的参数 curl_easy_setopt(卷曲,CURLOPT_WRITEDATA,P)

下面是传递一个缓冲区结构字符串{* PTR code的片段; LEN} 的回调函数,并使用realloc()的增长在每次调用该缓冲区。

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;卷曲/ curl.h>结构串{
  字符* PTR;
  为size_t LEN;
};无效init_string(结构串* S){
  S-GT&; LEN = 0;
  S-GT&; PTR =的malloc(S-GT&; LEN + 1);
  如果(S-GT&; PTR == NULL){
    fprintf中(标准错误的malloc()失败\\ n);
    出口(EXIT_FAILURE);
  }
  S-GT&; PTR [0] ='\\ 0';
}为size_t writefunc(无效* PTR,为size_t长度size_t nmemb个,结构串* S)
{
  为size_t new_len = S-> LEN +尺寸* nmemb个;
  S-GT&; PTR =的realloc(S-GT&; PTR,new_len + 1);
  如果(S-GT&; PTR == NULL){
    fprintf中(标准错误,realloc()的失败\\ n);
    出口(EXIT_FAILURE);
  }
  的memcpy(S-GT&; PTR + S-> LEN,PTR,大小* nmemb个);
  S-GT&; PTR [new_len] ='\\ 0';
  S-GT&; LEN = new_len;  返回大小* nmemb个;
}INT主要(无效)
{
  卷曲*卷曲;
  卷曲code资源;  卷曲= curl_easy_init();
  如果(卷曲){
    结构字符串s;
    init_string(安培; S);    curl_easy_setopt(卷曲,CURLOPT_URL,curl.haxx.se);
    curl_easy_setopt(卷曲,CURLOPT_WRITEFUNCTION,writefunc);
    curl_easy_setopt(卷曲,CURLOPT_WRITEDATA,&安培; S);
    RES = curl_easy_perform(卷曲);    的printf(%S \\ n,s.ptr);
    免费(s.ptr);    / *总是清理* /
    curl_easy_cleanup(卷曲);
  }
  返回0;
}

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

#include <stdio.h>
#include <curl/curl.h>

int main(void)
{
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "curl.haxx.se");
    res = curl_easy_perform(curl);

    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  return 0;
}

thanks, I solved it like this:

#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>

function_pt(void *ptr, size_t size, size_t nmemb, void *stream){
    printf("%d", atoi(ptr));
}

int main(void)
{
  CURL *curl;
  CURLcode res;
  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "curl.haxx.se");
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, function_pt);
    curl_easy_perform(curl);
    curl_easy_cleanup(curl);
  }
  system("pause");
  return 0;
}

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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

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;
}