#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; | |
} |
@alghanmi Works!!!!! your the best!!!!
Hello !
The curl example perfectly works ! What are the role of the "static size_t WriteCallback" function, the "if(curl)" and the "curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback)" ?
Thank you for your help.
This callback function will be called when receive the http response from the server.
You need to pass a function to handle the response stored in contents. The CURLOPT_WRITEDATA is set the fourth param in the cb function. You can write the response to this buffer and access it at your user context.
Note that you can't pass a std::string
as url parameter for curl_easy_setopt
:
std::string url = "http://www.google.com";
curl_easy_setopt(curl, CURLOPT_URL, url); // this will fail without triggering any compilation error (in g++, at least)
curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); // this will work
how can i use PUT or POST option in code? thank you!
when i tried url.c_str() I got segmantation fault error. passing readBuffer by reference is also working in my app.
url has to be converted using url.c_str()
Hi,
when i am giving wrong URL then it block with line
res = curl_easy_perform(curl);
is there any idea ,please suggest me.
Thanks in advance
again posting more
int sendMsg(string toSend){
int status = -1;
CURL *curl;
CURLcode res;
string response;
string url = "http://129.2.2.2/test.php";
string data = "name="+toSend;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(curl){
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
res = curl_easy_perform(curl); /------------------------ block here-------------------/
if(res == 0)
{
}
}
}
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.
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
To run this example, you need to have the curl headers installed
The only caveat is that when dealing with SSL connections, they need to use the OpenSSL libraries.