#include <iostream> | |
#include <string> | |
#include <curl/curl.h> | |
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) | |
{ | |
((std::string*)userp)->append((char*)contents, size * nmemb); | |
return size * nmemb; | |
} | |
int main(void) | |
{ | |
CURL *curl; | |
CURLcode res; | |
std::string readBuffer; | |
curl = curl_easy_init(); | |
if(curl) { | |
curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com"); | |
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); | |
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); | |
res = curl_easy_perform(curl); | |
curl_easy_cleanup(curl); | |
std::cout << readBuffer << std::endl; | |
} | |
return 0; | |
} |
res = curl_easy_perform(curl);
What is the use of "CURLcode res" variable? you have not used it further in above program? And what is the returning value to res variable?
Please help me..
This code did work right out of the box for me.
With so many projects there are always little details.
This is a great example.
The follow-up message that includes the compiler line cinches it. -lcurl was essential to the compilation.
How can i run this code in code block ?
Thank you so much for writing this as an example!
Hello,
For those folks who are looking for cURL + HTTPS - curl team has nice example on their project page:
Also there are tons of useful code snippets for many use cases: https://curl.haxx.se/libcurl/c/example.html
Where would one find the curl.h
file in curl's source code? Is it on GitHub by any chance?
Incredible code nevertheless!
Works perfectly
again posting more
int sendMsg(string toSend){
}
}
it blocked at line res = curl_easy_perform(curl);
if i am giving correct url then working fine but blocked with wrong url. it should block, it should return error code.
please provide me suggestion.