C libcurl gets output to a string

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

#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; 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; } 
+73
c libcurl
Feb 24 '10 at 21:13
source share
3 answers

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

The callback will take a user argument, which you can set with curl_easy_setopt(curl, CURLOPT_WRITEDATA, p)

Here is the code snippet that passes the buffer struct string {*ptr; len} struct string {*ptr; len} callback functions and increment this buffer every time it is called with 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; } 
+93
Feb 24 '10 at 21:43
source share
β€” -

The next answer is a C ++ way of doing this using std::string instead of std::string with a null character at the end. It still uses the callback function (there is no way around this), but also handles the allocation error with try / catch.

 #include <iostream> #include <string> #include <curl/curl.h> size_t CurlWrite_CallbackFunc_StdString(void *contents, size_t size, size_t nmemb, std::string *s) { size_t newLength = size*nmemb; try { s->append((char*)contents, newLength); } catch(std::bad_alloc &e) { //handle memory problem return 0; } return size*nmemb; } int main() { CURL *curl; CURLcode res; curl_global_init(CURL_GLOBAL_DEFAULT); curl = curl_easy_init(); std::string s; if(curl) { curl_easy_setopt(curl, CURLOPT_URL, "curl.haxx.se"); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); //only for https curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); //only for https curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWrite_CallbackFunc_StdString); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); curl_easy_setopt (curl, CURLOPT_VERBOSE, 1L); //remove this to disable verbose output /* Perform the request, res will get the return code */ res = curl_easy_perform(curl); /* Check for errors */ if(res != CURLE_OK) { fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); } /* always cleanup */ curl_easy_cleanup(curl); } std::cout<<s<<std::endl; std::cout<< "Program finished!" << std::endl; } 
+25
Apr 04 '16 at 11:48 on
source share

From the manual read here: http://curl.haxx.se/libcurl/c/curl_easy_setopt.html I think you need some CURL_SETOPT calls, the first of which is the URL you want to handle, the second is something like:

 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, function_ptr); 

Where function_ptr matches this signature:

 size_t function( void *ptr, size_t size, size_t nmemb, void *stream) 

What happens here, you mean the callback function that libcurl will call when it has any output to write from any transfer you made. You can get it for automatic writing to a file or pass it a pointer to a function that will process the output itself. With this function, you can collect various output lines into one part, and then use them in your program.

I'm not sure what other options you can set / what else affects how you want your application to behave, so have a look at this page.

+7
Feb 24 '10 at 21:40
source share



All Articles