Curl common functions Introduction

A, LibCurl basic programming framework

Based LibCurl in the program, mainly in the form of callback function (callback function) to complete the transfer tasks, users set the various parameters and callbacks before the transfer is initiated when the user invokes the condition libcurl callback function to achieve specific functions. The following is the use libcurl to complete the transfer task process:
  1. Call curl_global_init () to initialize libcurl
  2. calls curl_easy_init to () function to get the pointer type easy interface
  3. Invoke of curl_easy_setopt () Set transfer option
  according of curl_easy_setopt () transfer option is set to achieve a user callback function to accomplish specific tasks
  5. call curl_easy_perform () function to complete the transfer task
  6. call curl_easy_cleanup () to release memory
  7. Call curl_global_cleanup () to release all the resources
provided in the whole process over curl_easy_setopt ( ) is the most critical parameter, almost all programs have to use the libcurl it.
Second, some of the basic functions
1.CURLcode curl_global_init (long flags);
Description:
  This function can only be used once. (In fact, still reusable after calling curl_global_cleanup function)
  If this function is not called when curl_easy_init function call, it is automatically called by speaking libcurl library, so the next best multi-threaded initiative to call this function in order to prevent multiple threads when curl_easy_init invocation.
Note: Although libcurl is thread-safe, but curl_global_init can not guarantee thread-safe, so do not have to call curl_global_init in each thread should call the function in the main thread.
Parameters: flags
  CURL_GLOBAL_ALL // initialize all possible calls.
  CURL_GLOBAL_SSL // initialize supports Secure Sockets Layer.
  Win32 CURL_GLOBAL_WIN32 // initialize the socket library.
  CURL_GLOBAL_NOTHING // no additional initialization.
2 void curl_global_cleanup (void);
Description: At the end libcurl used for clean-up work to do to curl_global_init. Similar to the function of close.
Note: Although libcurl is thread-safe, but curl_global_cleanup can not guarantee thread-safe, so do not have to call curl_global_init in each thread should call the function in the main thread.
3 CURL * curl_easy_init ();
Description:
  curl_easy_init used to initialize a CURL pointer of (somewhat like a pointer to the same type of return FILE) appropriate use curl_easy_cleanup clean up at the end of the function call.
  General curl_easy_init mean a session start it. It returns a easy_handle (CURL * objects) are generally used in functions easy series.
4 void curl_easy_cleanup (CURL * handle)
; Description:
  . This call is used to end a conversation with curl_easy_init combined with 
parameters:
  the CURL pointer type.
5 CURLcode curl_easy_setopt (CURL * handle,
CURLoption option, parameter); Description: This function is most important almost all of the curl program must use it frequently it tells curl library program will have to see how the behavior of such a.... web page html code and other parameters (this function somewhat like ioctl function):.
  1 CURL pointer of type
  2 CURLoption various types of options (both defined in the curl.h Curry, man can also view)
  3 this parameter is the parameter either a pointer to a function, can also be a pointer to an object, it can be a long type variable. it is what it depends on the second parameter.
  the value of this parameter CURLoption a lot. You can view the specific man pages.
6 CURLcode curl_easy_perform(CURL *handle);
Description: This function is called after initialization CURL type of pointer and curl_easy_setopt completed just literally said perform like a stage set us..
  The Option up and running parameters:.
  CURL type of pointer.
Three, curl_easy_setopt function introduces some options
  parameters This section describes curl_easy_setopt in with http relevant. This function is a very important function curl, curl all the settings are done in the function, the function of numerous setting options, note that this section only describes some common options.
1. CURLOPT_URL: set access URL
2. CURLOPT_WRITEFUNCTION, CURLOPT_WRITEDATA
callback function prototype: size_t function (PTR void *, size_t size, size_t of nmemb, void * Stream);  function libcurl data received after the call, so the function of the stored data do functions such as handling download file.
 CURLOPT_WRITEDATA source stream pointer indicating CURLOPT_WRITEFUNCTION function.
Note: Set the callback function, libcurl will provide a default callback function if you do not pass CURLOPT_WRITEFUNCTION attribute to easy handle, it simply will print the received data to standard output.
   You can also CURLOPT_WRITEDATA attribute to the default callback function passing a pointer to a file has been opened for output data to a file.
3.CURLOPT_HEADERFUNCTION, CURLOPT_HEADERDATA
callback function prototype is size_t function (void * ptr, size_t size, size_t nmemb, void * stream); http header after libcurl data received once the function call.
  CURLOPT_WRITEDATA pass a pointer to libcurl, the pointer to indicate the source stream pointer CURLOPT_HEADERFUNCTION function.
4.CURLOPT_READFUNCTION CURLOPT_READDATA
will call the function CURLOPT_READFUNCTION specified when libCurl need to read data to a remote host,
The function prototype is: size_t function (void * ptr, size_t size, size_t nmemb, void * stream).
  CURLOPT_READDATA stream pointer indicates that the source of CURLOPT_READFUNCTION function prototype.
5.CURLOPT_NOPROGRESS, CURLOPT_PROGRESSFUNCTION, CURLOPT_PROGRESSDATA: with data transmission parameters related to the progress.
  Libcurl is invoked CURLOPT_PROGRESSFUNCTION specified function normally once per second, in order to make CURLOPT_PROGRESSFUNCTION is called, CURLOPT_NOPROGRESS must be set to false,
  CURLOPT_PROGRESSDATA specified parameter specifies the first parameter as a function of CURLOPT_PROGRESSFUNCTION.
6.CURLOPT_TIMEOUT, CURLOPT_CONNECTIONTIMEOUT:
  CURLOPT_TIMEOUT since the transmission time, CURLOPT_CONNECTIONTIMEOUT connection waiting time is provided
7.CURLOPT_FOLLOWLOCATION
  set relocation URL
8.CURLOPT_RANGE: CURLOPT_RESUME_FROM:
  HTTP settings. Char * CURLOPT_RANGE specify parameters passed to libcurl, for RANGE http header field in the domain, for example:
  represents the first 500 bytes: bytes = 0-499
  represents the second 500 bytes: bytes = 500-999
  indicates that the last 500 byte: bytes = -500
  represents the range of the subsequent 500 bytes: bytes = 500-
  first and last bytes: bytes = 0-0, -1
  specify several ranges: bytes = 500-600,601-999
  CURLOPT_RESUME_FROM a long argument passed to libcurl, specify the offset you want to start passing.
Four,   curl_easy_perform Function Description (error status code)
This function is to complete all the options curl_easy_setopt specified in this section focuses on curl_easy_perform return value. Return 0 means everything ok, representatives of non-zero error. The main error code Description:
1.CURLE_OK 
    task is completed everything
2.CURLE_UNSUPPORTED_PROTOCOL
    unsupported protocol, designated by the head of the URL
3.CURLE_COULDNT_CONNECT
    can not connect to a remote host or proxy
4.CURLE_REMOTE_ACCESS_DENIED
    access was denied
5.CURLE_HTTP_RETURNED_ERROR
    Http return an error
6 .CURLE_READ_ERROR
    read local file errors
To get the detailed error description string, you can const char * curl_easy_strerror (CURLcode errornum)  made this function.
Five, libcurl HTTP header used
    when transmitting http request using libcurl, it will automatically add some http header. We can replace manually CURLOPT_HTTPHEADER properties, add or delete HTTP message headers.
    Host
    HTTP1.1 (most http1.0) versions require clients to request this information first.
    Pragma
    "NO-Cache." It means you can not buffer data.
    The Accept
    "* / *." It denotes allowed to receive any type of data.
    Expect
    When submitting a request to the server in HTTP POST manner, the message header libcurl is set as "100-continue", it requires the server to process the request before the formal, returns a "OK" message. If the POST data is small, libcurl may not set the message header.
  Modify header

    HTTP protocol provides the message header, the request header is used to tell the server how to handle the request; response message header tells the browser how to process the received data. In libcurl, you can freely add these headers:

  struct curl_slist *headers=NULL; /*init to NULL is important*/
  headers = curl_slist_append (headers, "the Accept: * / *"); 
  headers = curl_slist_append (headers, "Pragma: NO-Cache");
  headers = curl_slist_append (headers, "the Host: HTTP 1.1");
  of curl_easy_setopt (easyhandle, CURLOPT_HTTPHEADER, headers);
  curl_easy_perform (easyhandle); / * Transfer HTTP * /
  / * Free the header List * /; curl_slist_free_all (headers)
to delete the message header
  for an existing message header, provided it is empty, libcurl transmission request this message will not be submitted at the same head:
  headers = curl_slist_append (headers, "the Accept:");
Seven multi-threading issues
    first a basic principle: that should never share the same libcurl handle (CURL * objects) between threads, or whether it is easy handle multi handle (this article only describes easy_handle). A thread can only use a handle.
Eight, libcurl does not work when
  a transmission failure is always a reason. You might set the wrong attribute some of libcurl or not properly understand the meaning of certain properties, or the remote host to return some content can not be properly resolved.
   There is a golden rule to address these problems: The CURLOPT_VERBOSE property to 1, libcurl outputs some details of the communication process. If you are using the http protocol, the first request / response header will be output. The CURLOPT_HEADER set to 1, the header information will appear in the content of the message.
   Of course, it is undeniable that, libcurl there bug.
   If you are more understanding of the relevant agreements, in the use of libcurl, less likely to make mistakes.

Nine, on a password
  when the client sends a request to the server, many protocols require a user name and password. libcurl provides several ways to set them.
   Some protocol support in the URL directly specify a username and password, like: protocol: // user: [email protected]/path/. libcurl correctly identify this user name and password in the URL and performs a corresponding operation. If there are special characters in the user name and password you provided, first of all it should be URL encoded.
   You can also set up a user name and password by CURLOPT_USERPWD property. Parameters such as the format "user: password" string:
   curl_easy_setopt (easy_handle, CURLOPT_USERPWD, "user_name: password")
   sometimes when accessing the proxy server, may from time to time requires a user name and password for user authentication. In this case, libcurl provides another property CURLOPT_PROXYUSERPWD:
   curl_easy_setopt (easy_handle, CURLOPT_PROXYUSERPWD, "user_name: password"); 
   In the UNIX platform, access to the FTP user name and password may be stored in $ HOME / .netrc file. libcurl support to obtain a user name and password directly from the file:
   curl_easy_setopt (easy_handle, CURLOPT_NETRC, 1L); 
   when using SSL, may be required to provide a private key for secure transmission of data, the private key is set by CURLOPT_KEYPASSWD:
   curl_easy_setopt(easy_handle, CURLOPT_KEYPASSWD, "keypassword");

Ten, HTTP validation
   when using the HTTP protocol, a client there are many ways to provide authentication information to the server. The default authentication method is HTTP "Basic", it username and password in clear text, stored after Base64 encoded in the HTTP request header, to the server. Of course, this is not very safe.
   The current version of libcurl authentication methods are supported: basic, Digest, NTLM, Negotiate , GSS-Negotiate and SPNEGO. : (Translator sigh: Web engage in so many years, do not necessarily know Http authentication methods, it is ashamed.) You can set a specific authentication by CURLOPT_HTTPAUTH property
   ; curl_easy_setopt (easy_handle, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST
)    when sending authentication information to the proxy server , provided by authentication CURLOPT_PROXYAUTH:
   of curl_easy_setopt (easy_handle, CURLOPT_PROXYAUTH, CURLAUTH_NTLM);
   may be provided a variety of authentication (through bitwise aND), the use of 'CURLAUTH_ANY' may choose any libcurl will allow it supports authentication simultaneously. Or by a variety of authentication CURLOPT_HTTPAUTH CURLOPT_PROXYAUTH property settings, libcurl it will choose one that is the best way to communicate with the server at run time:
   of curl_easy_setopt (easy_handle, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST | CURLAUTH_BASIC); 
   // of curl_easy_setopt (easy_handle, CURLOPT_HTTPAUTH, CURLAUTH_ANY);

 

 

Guess you like

Origin www.cnblogs.com/zhangnianyong/p/11759810.html