int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
_getch();
return 0;
}
string contents = "";
I would like to save the result of the curl html content in a string, how do I do this?
It’s a silly question but unfortunately, I couldn’t find anywhere in the cURL examples for C++
thanks!
You will have to use
CURLOPT_WRITEFUNCTIONto set a callback for writing. I can’t test to compile this right now, but the function should look something close to;Then call it by doing;
After the call,
readBuffershould have your contents.Edit: You can use
CURLOPT_WRITEDATAto pass the buffer string instead of making it static. In this case I just made it static for simplicity. A good page to look (besides the linked example above) is here for an explanation of the options.Edit2: As requested, here’s a complete working example without the static string buffer;