C++ curl wrapper

I am spending some time writing a little app to index all of my books. As I am getting the data from amazon I needed a little curl wrapper for C++. As the libcurl or curl man page and the online resources take some time to look through here it is.
You just give it a URL and the class will return a string with the content of the URL or NULL if a error accrued. This can be saved into a file. For example to download a image.

Don't forget to add LDFLAGS=`curl-config --libs` in your Makefile

The .h file


1: /*
2: * DBook : Didi's book system
3: *
4: * $Id: GetDataCurl.h,v 1.1 2007/04/22 23:57:17 didi Exp $
5: *
6: * This class is used to download a webpage with curl
7: *
8: */

9:
10: #include <string>
11: #include <iostream>
12: #include "curl/curl.h"
13:
14: namespace dbook{
15:
16: class GetDataCurl{
17:
18: public:
19: std::string getDataFromWeb(std::string theUrl);
20:
21: private:
22: char bufferError[CURL_ERROR_SIZE];
23: std::string outputBuffer;
24:
25: /* The curl thing */
26: /* Has to be static because c doesn't understand C++ */
27: static int curlWriter
(
char *data, size_t size, size_t nmemb, std::string *outputBuffer);
28: };
29: }


And the .cpp File


1: /*
2: * DBook : Didi's book system
3: *
4: * $Id: GetDataCurl.cpp,v 1.1 2007/04/22 23:57:17 didi Exp $
5: *
6: * This is the main class for getting data from a http server
7: *
8: */

9:
10:
11: #include <string>
12: #include <iostream>
13: #include "curl/curl.h"
14:
15: #include "GetDataCurl.h"
16: #include <errno.h>
17:
18: namespace dbook{
19:
20: std::string GetDataCurl::getDataFromWeb(std::string theUrl){
21:
22: CURL *curl;
23: CURLcode result;
24:
25: /* Init */
26: curl = curl_easy_init();
27:
28: if (curl){
29: /* set up some stuff */
30: curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, bufferError);
31: curl_easy_setopt(curl, CURLOPT_URL, theUrl.c_str() );
32: curl_easy_setopt(curl, CURLOPT_HEADER, 0);
33: curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
34: curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriter);
35: curl_easy_setopt(curl, CURLOPT_WRITEDATA, &outputBuffer);
36:
37: result = curl_easy_perform(curl);
38:
39: /* Clean*/
40: curl_easy_cleanup(curl);
41:
42:
43: if (result == CURLE_OK){
44: return outputBuffer;
45: }else{
46: /* Something whent wront trying to download the page */
47: std::cerr << "Error: " << bufferError << std::endl;
48: return NULL;
49: }
50: }else{
51: /* Something whent wrong while trying to init curl */
52: perror("curl_easy_init failed");
53: return NULL;
54: }
55:
56: }
57:
58:
59: /* The curl thing */
60: int GetDataCurl::curlWriter
(
char *data, size_t size, size_t nmemb, std::string *outputBuffer){
61: /* Check for errors */
62: if (outputBuffer != NULL){
63: outputBuffer->append(data, size * nmemb);
64: }
65: return (size * nmemb);
66:
67: }
68:
69: }

4 comments:

izua said...

Hey dude, thanks a lot! I've been looking to integrate the curl library in C++, but it proved a complete pain in the ass.

Ah well, now all that's left are the ton of options :D Thanks again!

Unknown said...

Hi,

This is code is beautiful. Help me a lot ;)

Thanks for posting this.
--
vicent.

Unknown said...

Hey,

Thank you for posting this! Helped a lot.

Anonymous said...

Looks to me like there is some initialization that is expected somewhere for this to work, because I get an error message: Error: Protocol wget https not supported or disabled in libcurl