Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <curl/curl.h>
- // This function basically deals with the buffers of data returned when we make a HTTP GET request.
- size_t writefunc(char* data, size_t charsize, const size_t buffsize, std::string *ret)
- {
- const size_t realsize = charsize * buffsize;
- // Convert data into a C string
- char* cstr = new char[realsize+1];
- for(size_t i = 0; i < realsize; ++i)
- cstr[i] = data[i];
- cstr[realsize] = '\0';
- // Create the return value
- *ret += cstr;
- // We have to return this or else curl will throw an error.
- return realsize;
- }
- int main()
- {
- std::string ret; // Our return value to the GET request, basically the HTTP page.
- CURL *curl; // This will be our curl request.
- // Initialise curl.
- curl = curl_easy_init();
- if(!curl)
- {
- std::cerr << "Error initialising curl!" << std::endl;
- return 1;
- }
- // Curl options.
- curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
- curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, true);
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); // The function we use to deal with the stream of data.
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ret); // Arguments passed onto the write function.
- // Execute it.
- curl_easy_perform(curl);
- // Show the web page in text.
- std::cout << ret << std::endl;
- // Clean up.
- curl_easy_cleanup(curl);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment