There are many introductions on curl. I will not describe it in detail here. I will mainly talk about simple applications.
I recently studied curl C API in Linux. The latest version is 7.17, and curl C API documentation is rich. The only difference is that it takes some time to search for it. The c api of curl and the php api of curl have the same function interface and function interface. Therefore, it should be well understood if you have php api experience.
C API Based on curl writes an extended C ++ Singleton class (of course, curl also has c ++ API). This single class only encapsulates HTTP requests and provides post, get method, and get the return value in the request URL (saved to the string object), it is easy to extend to other protocols.
Curlplus. h file
# Ifndef _ curlplus_h __
# DEFINE _ curlplus_h __
# Ifndef _ curl_curl_h
# Include <curl/curl. h>
# Endif
# Ifndef _ curl_easy_h
# Include <curl/easy. h>
# Endif
# Include <memory>
# Include <string>
Using namespace: STD;
Namespace CP
{
Class curlplus
{
Public:
Static curlplus & get_instance ();
String post (const string & URL, const string & content) const;
String get (const string & URL) const;
Protected:
Curlplus (void );
~ Curlplus (void );
Curlplus (const curlplus &);
Curlplus & operator = (const curlplus &);
Static int writer (char * data, size_t size, size_t nmemb, STD: string * writerdata );
PRIVATE:
Static auto_ptr <curlplus> _ instance;
Inline void _ setcurlopt (curl * curl, const string & URL) const;
// Default timeout 300 s
Static const int _ defaulttimeout = 300;
Static string buffer;
Friend class auto_ptr <curlplus>;
};
}
# Endif
Curlpuls. CC File
# Ifndef spivot_curlplus_h __
# Include "curlplus. H"
# Endif
Using namespace STD;
Using namespace CP;
Auto_ptr <curlplus> curlplus: _ instance;
String curlplus: buffer;
Static char errorbuffer [curl_error_size];
Curlplus & curlplus: get_instance ()
{
If (_ instance. Get () = NULL)
{
_ Instance. Reset (New curlplus ());
}
Return * _ instance. Get ();
}
Int curlplus: writer (char * data, size_t size, size_t nmemb, string * writerdata)
{
If (writerdata = NULL)
Return 0;
Int Len = size * nmemb;
Writerdata-> append (data, Len );
Return Len;
}
Curlplus: curlplus (void)
{
Curlcode code = curl_global_init (curl_global_all );
If (code! = Curle_ OK)
{
Cout <"curl_init failed, error code is:" <code;
}
}
Curlplus ::~ Curlplus (void)
{
Curl_global_cleanup ();
}
String curlplus: Post (const string & URL, const string & content) const
{
Buffer = "";
Curl * curl = curl_easy_init ();
If (curl = NULL)
{
Cout <"curl_easy_init failed ";
}
_ Setcurlopt (curl, URL );
Curl_easy_setopt (curl, curlopt_post, 1 );
Curl_easy_setopt (curl, curlopt_postfields, content. c_str ());
Curlcode code = curl_easy_perform (curl );
If (code! = Curle_ OK)
{
Cout <"curl_easy_perform failed:" <code;
}
Curl_easy_cleanup (curl );
Return buffer;
}
String curlplus: Get (const string & URL) const
{
Buffer = "";
Curl * curl = curl_easy_init ();
If (curl = NULL)
{
Cout <"curl_easy_init failed ";
}
_ Setcurlopt (curl, URL );
Curlcode code = curl_easy_perform (curl );
If (code! = Curle_ OK)
{
Cout <"curl_easy_perform failed:" <code;
}
Curl_easy_cleanup (curl );
Return buffer;
}
Void curlplus: _ setcurlopt (curl * curl, const string & URL) const {
Curl_easy_setopt (curl, curlopt_errorbuffer, errorbuffer );
Curl_easy_setopt (curl, curlopt_header, 0 );
Curl_easy_setopt (curl, curlopt_url, URL. c_str ());
Curl_easy_setopt (curl, curlopt_timeout, _ defaulttimeout );
// Curl_easy_setopt (curl, curlopt_verbose, true );
Curl_easy_setopt (curl, curlopt_writefunction, curlplus: writer );
Curl_easy_setopt (curl, curlopt_writedata, & buffer );
}
Here are a few notes
1: curlcode curl_global_init (long flags); function, which needs to be called once globally (multiple calls are acceptable, but not necessary ), therefore, this is also the reason for designing curlplus into a single class. The curl_global_init function is called at least once before other libcurl functions are called, and the program needs to call curl_global_cleanup at last for cleanup.
Parameter: flags
Curl_global_all initialize everything possible. This sets all known bits.
Curl_global_ssl initialize SSL
Curl_global_win32 initialize the Win32 socket libraries.
Curl_global_nothing initialise nothing extra. This sets no bit.
Curlcode is an enum. When curlcode is curle_ OK, it indicates that the function is successfully executed; otherwise, it fails. For specific error causes, see the definition in the <curl/curl. h> file.
2: curl_easy_init-start a libcurl easy session
Curl_easy_init is used to initialize a curl pointer (some are the same as returning a file pointer ). use the curl_easy_cleanup function at the end of the call. generally, curl_easy_init indicates the start of a session. the return value is curl *, and the curl_easy_init function is thread-related. That is to say, you cannot call the curl pointer created by another thread through curl_easy_init in one thread.
3: curlcode curl_easy_setopt (curl * handle, curloption option, parameter );
Description: This function is the most important. almost all curl programs use it frequently. it tells the curl library. what will the program do. for example, you can view the HTML code of a webpage ., to learn more about curl behavior, you must have a sufficient understanding of curloption. For details, refer
Http://curl.haxx.se/libcurl/c/curl_easy_setopt.html
The following two types are not easy to understand: curlopt_writefunction and curlopt_writedata.
Curl_easy_setopt (curl, curlopt_writefunction, curlplus: writer );
Set a callback function in the format
Size_t function (void * PTR, size_t size, size_t nmemb, void * stream );
PTR, pointer to the returned data
Size: the size of each part of the returned data.
Nmemb, number of returned data blocks (the actual size of the data string is returned here is size * nmemb)
Stream, which is the buffer pointer in curl_easy_setopt (curl, curlopt_writedata, & buffer.
In the preceding example, the buffer is set as a string object. Therefore, the callback function writer contains writerdata-> append (data, Len );
4: curlcode curl_easy_perform (curl * handle );
Execute remote request
References
Http://curl.haxx.se/
Http://curl.haxx.se/lxr/source/docs/examples/