Output libcurl to a variable instead of a text file

Having not got curlpp to work in C ++, I decided to start using libcurl with C instead (for now). Being completely new to C and C ++, this is a bit confusing. I'm not even sure if I can support the C and C ++ functions separately, but as far as I know, this is pure C.

With the help of a friend, I managed to write the output (the contents of the page that curled up) to a text file, but instead I want to put it in a string variable, so I can use the output in other parts of the code. I could just reopen the text file and read its contents, but this is stupid, I want to stop writing to the file and immediately save the string variable.

Recording function

 /* the function to invoke as the data received */ size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) { size_t written; written = fwrite(ptr, size, nmemb, stream); return written; } 

Whole code

 #include <iostream> #include "curl/curl.h" #include <stdio.h> #include <stdlib.h> #include <iostream> using namespace std; /* the function to invoke as the data recieved */ size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) { size_t written; written = fwrite(ptr, size, nmemb, stream); return written; } int main(int argc, char *argv[]) { CURL *curl; FILE *fp; CURLcode res; curl = curl_easy_init(); char outfilename[FILENAME_MAX] = "C:\\Users\\admin\\Downloads\\bbb.txt"; if(curl) { char *response = NULL; fp = fopen(outfilename,"wb"); curl_easy_setopt(curl, CURLOPT_URL, "http://www.*hidden*.org/wp-test/lalala.txt"); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); res = curl_easy_perform(curl); curl_easy_cleanup(curl); fclose(fp); } return 0; } 

I also hoped that someone would be able to talk in detail about how this function is used. I am using (from php and vb.net) a function similar to this:

 function1(ThisIsAvariable,ThisIsAvariableToo) { if ThisIsAvariable = something { Print "gogopowerrrangers" *append* ThisIsAvariableToo } }; 

which will then be used as:

 function1(PassThisAsVariable1,PassThisAsVariable2); 

But in the above code, the function

 size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) 

Just called like that

 write_data 

As you can see here:

 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); 

So what is all this for?

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

Does curl automatically "populate" certain C / C ++ with other functions than most other languages?

+6
source share
1 answer

You can use a string stream to store data in memory. You must create a stream of strings in main and pass a pointer to it so that it curls, for example, you are currently passing a pointer to FILE .

 std::ostringstream stream; curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &stream); // call curl like before // ... // then get result as a std::string: std::string output = stream.get(); 

The corresponding write_data function will look like this:

 size_t write_data(char *ptr, size_t size, size_t nmemb, void *userdata) { std::ostringstream *stream = (std::ostringstream*)userdata; size_t count = size * nmemb; stream->write(ptr, count); return count; } 

And on the function call, it works basically the same as in other languages. You must specify the parameters of the function if you call it. The difference in this case is that in curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data) function write_data not called. write_data instead specified as a parameter to curl_easy_setopt , which curl_easy_setopt curl to use this function when it needs to write data.

Later, when curl receives the data and wants to write this data, it will call write_data with all the relevant parameters.

+10
source

Source: https://habr.com/ru/post/900842/


All Articles