One of the key functions of the Curl_easy_setopt-curl Library

Source: Internet
Author: User
Tags function prototype http cookie http post ftp transfer

Function Prototypes:
#include <curl/curl.h>
Curlcodecurl_easy_setopt (CURL *handle, curloption option, parameter);



Description
This function is used to tell libcurl what action to perform. The function has 3 parameters (the function can be set to many options):
The 1th parameter handle is the handle returned by Curl_easy_init (), the 2nd parameter is the option (curloption) that can be set, and the 3rd parameter is the parameter associated with the 2nd option, which can be a long or a function pointer (func tion pointer), can also be a pointer to an object (object pointer), or a curl_off_t type that must be determined by the option value (curloption).

Description of specific options (categories and sub-categories, this note for personal understanding and translation of the instruction manual, according to the application of the constant updating of the description, the classification sequence does not follow the instruction manual):

Network options:

1. Curlopt_url
This option is followed by the actual URL to be processed, which is a string or parameter pointer ending with '/' (see: Http://www.groad.net/bbs/read.php?tid-1641.html for a simple example of this parameter). If the URL parameter is not written on the protocol header (such as "http:/" or "ftp://," etc.), then the function will guess for itself which service protocol is used on the host. If the address you are giving is an unsupported protocol, then the Curl_easy_perform () function or the Curl_multi_perform () function will be executed after the Libcurl returns an error (Curle_unsupported_ PROTOCOL). This option is the only option that must be set before the Curl_easy_perform () call.

2. Curlopt_postfieldsize
This option requires that the 3rd parameter, parameter, is a void * pointer, which points to a piece of data to POST to the HTTP server, which is filled in according to the server's specific requirements. This option uses a reference example: http://www.groad.net/bbs/read.php?tid-3909.html

3. Curlopt_writefunction
When using this option, the callback function in the 3rd parameter must be the following function prototype:

size_t function (char *ptr, size_t size, size_t nmemb, void *userdata);


When a session is started, the callback function is called once the data that needs to be received is detected. The data size that PTR points to is derived from the product of size and nmemb. The function finally needs to return the size of the received data. If you do not use this function, the received data will be printed directly to the terminal; using this function, the received data is stored in the area held by PTR and can be used to hold the received data.

4. Curlopt_useragent
This option requires the passing of a string pointer ending with ' user-agent ', which is used to send the HTTP header information to the server when requested, some servers need to detect this information, and if no user-agent is set, the server rejects the request. After setting up, you can fool the server to check for this.

5. Curlopt_writedata

With this option, the 3rd parameter is passed to the callback function specified when using the Curlopt_writefunction option (the 4th parameter) as a pointer to the user data. If you do not want to save the data with a callback function, you can use the Curlopt_writedata option when the 3rd parameter of the function must be a file pointer, and the function will automatically write the received data to the stream to which this file pointer points.

6. Curlopt_verbose
When you use this option and the 3rd parameter is 1 o'clock, the Curl library displays detailed operational information. This is very helpful for debugging the program.

7. Curlopt_nobody
With this option, if the 3rd parameter is set to 1, then the body content part is not included in the output. This is only for all content in the transmission, including the "head" and "subject content" of the two parts of the agreement. such as an HTTP (S) server, in which case the Libcurl library will emit only one header request.

8. Curlopt_header
When this option is used, the 3rd parameter is set to 1, and the Curl Library is notified to include both "head" and "subject content" in two parts of the output. This option is only for protocols that contain both the "head" and "Subject content" sections (such as HTTP).

9. Curlopt_headerfunction
This option is similar to the 3rd option above Curlopt_writefunction, which executes a callback function as soon as it receives the header information. It is important to note that the header processed in the callback function contains the header information of all the responses received, not just the last response. If you need to handle one of the headers, then you need to differentiate between the headers that you collect.

Curlopt_writeheader and Curlopt_headerdata.
These two options are the same meaning. They are the same as the Curlopt_writedata option in 5th, which means passing the 4th parameter to the callback function when the header information is received and a callback function is called.

Curlopt_infilesize.
When uploading files to the server, this option is used to tell the size of the file The Curl Library expects to upload. When you use this option, you should give the function a long variant of the 3rd argument. If you are using an SCP transfer, this option enforces the use of curlopt_infilesize_large.

Curlopt_infilesize_large.
This option is the same as the Curlopt_infilesize function, but it requires that the 3rd parameter of the function must be a curl_off_t type. curl_off_t is a int64_t type, and int64_t is defined in stdint.h:

if __wordsize = = 64

typedef long int int64_t;

# Else

__extension__

typedef long long int int64_t;


As defined by the above, general, if it is a 32-bit platform, it is a long long, 64-bit, if it is a 64-bit platform, it is a long type, also 64 bits. In short, it's a 64-bit.

Curlopt_quote and Curlopt_postquote.
These two options are similar in functionality, and they all have a common denominator of passing commands to FTP or SFTP. These commands should be stored in the struct slist linked list and used with the Curl_slist_append () function to package the commands and send them together.
Their difference is that the Curlopt_quote option requires the command to be sent to the library before the FTP transfer request, and Curlopt_postquote can be sent after the FTP transfer request is sent. For example, the following order of execution cannot achieve the purpose:

1. ... ...

2 Curl_easy_perform (curl);

3 curl_easy_setopt (Curl, Curlopt_quote, headerlist);

4 Curl_easy_perform (curl);

5 ...



The above assumes 1th curl_easy_perform (curl); Statement has sent an FTP transfer request, and then sending the command with Curlopt_quote is an error, the Curlopt_postquote option should be used here. Use the Curlopt_quote option to send it using the Curl_easy_perform () function before sending the FTP transfer request again with a curl_easy_perform (). This means that you need to perform two curl_easy_perform () functions. This is not required with the Curlopt_postquote option, as long as the option is set, and then only once the Curl_easy_perform () function is executed.

Curlopt_readfunction and Curlopt_readdata.
These two options are similar to the above Curlopt_writefunction and Curlopt_writedata. In Curlopt_readfunction's callback function, the 1th parameter ptr pointer is used to receive data passed from the 4th parameter (this parameter is often a file flow pointer), and this parameter is passed when using the Curlopt_readdata option.

Curlopt_upload.
When you use this option, the 3rd parameter is set to 1, which means that you are ready to upload the file. This parameter is often used in conjunction with Curlopt_readdata,curlopt_infilesize_large, as well as the curlopt_infilesize options. If you are using the HTTP protocol, use the PUT method to upload, unless otherwise specified.

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////

Curlopt_header: Set to 1, then include the HTTP HEADER in the returned content;

Curlopt_followlocation: Set to 0, it will not automatically 301,302 jump;

*curlopt_infilesize: When you upload a file to a remote site, this option tells PHP the size of your upload file.
*curlopt_verbose: If you want curl to report every unexpected thing, set this option to a value other than 0.
*curlopt_header: If you want to include a header in the output, set this option to a value other than 0.
*curlopt_noprogress: If you do not have PHP display a process bar for the Curl transfer, set this option to a value other than 0.

Note: PHP automatically sets this option to a value other than 0, you should only change this option for debugging purposes.

*curlopt_nobody: If you do not want to include the body part in the output, set this option to a value other than 0.
*curlopt_failonerror: If you want PHP to not display when an error occurs (HTTP code returns greater than or equal to 300), set this option to one person other than 0 value. The default behavior is to return a normal page, ignoring the code.
*curlopt_upload: If you want PHP to prepare for uploading, set this option to a value other than 0.
*curlopt_post: If you want PHP to do a regular HTTP POST, set this option to a value other than 0. This post is an ordinary application/x-www-from-urlencoded type, most of which is used by HTML forms.
*curlopt_ftplistonly: Set this option to a value other than 0, PHP lists the list of directory names for FTP.
*curlopt_ftpappend: Set this option to a value other than 0, PHP will apply the remote file instead of overwriting it.
*CURLOPT_NETRC: Set this option to a value other than 0, PHP will look in your ~./netrc file for the user name and password of the remote site you want to connect to.
*curlopt_followlocation: Set this option to a non-0 value (like "Location:") header, the server will send it as part of the HTTP header (note that this is recursive, PHP will be sent as "location:" the head).
*curlopt_put: Set this option to use HTTP to upload a file for a value other than 0. To upload this file you must set the Curlopt_infile and Curlopt_infilesize options.
*curlopt_mute: Set this option to a value other than 0, PHP will be completely silent for the curl function.
*curlopt_timeout: Sets a long shape number, as the maximum continuation of how many seconds.
*curlopt_low_speed_limit: Sets a long shaping number to control how many bytes are transferred.
*curlopt_low_speed_time: Sets a long shape number that controls how many seconds to transmit curlopt_low_speed_limit the specified number of bytes.
*curlopt_resume_from: Pass a long shaping parameter that contains the byte offset address (the start form you want to transfer to).
*curlopt_sslversion: Pass a long parameter that contains the SSL version. The default PHP will be determined by its own efforts, and in more security you must set it up manually.
*curlopt_timecondition: Passes a long parameter that specifies how the Curlopt_timevalue parameter is handled. You can set this parameter to Timecond_ifmodsince or timecond_isunmodsince. This is used only for HTTP.
*curlopt_timevalue: Passes a number of seconds from 1970-1-1 onwards to the present. This time will be used by the Curlopt_timevalue option as the specified value, or by default timecond_ifmodsince.

The values of the following options will be used as strings:

*curlopt_url: This is the URL address you want to retrieve with PHP. You can also set this option when initializing with the Curl_init () function.
*curlopt_userpwd: Pass a shape like [Username]:[password] style string, function PHP to connect.
*curlopt_proxyuserpwd: Pass a string that is formatted as [Username]:[password] to connect to the HTTP proxy.
*curlopt_range: Pass a range that you want to specify. It should be in the "X-y" format, except for X. or Y. HTTP delivery also supports several intervals, separated by a phrase (x-y,n-m).
*curlopt_postfields: A string that passes all data as an HTTP "POST" operation.
*curlopt_referer: A string that contains a "REFERER" header in an HTTP request.
*curlopt_useragent: A string that contains a "user-agent" header in an HTTP request.
*curlopt_ftpport: Pass an IP address that contains the FTP "POST" instruction used. This post command tells the remote server to connect to the IP address we specified. This string can be an IP address, a host name, a network interface name (under Unix), or '-' (using the system default IP address).
*curlopt_cookie: Passes a header connection that contains an HTTP COOKIE.
*curlopt_sslcert: Pass a string containing a certificate in PEM format.
*CURLOPT_SSLCERTPASSWD: Pass a password that contains required to use the Curlopt_sslcert certificate.
*curlopt_cookiefile: A string that passes the name of a file that contains cookie data. This cookie file can be a Netscape format, or an HTTP-style header that is stockpiled in a file.
*curlopt_customrequest: When an HTTP request is made, passing a character is used by get or head. For a delete or other operation is beneficial, more pass a string to is used instead of GET or HEAD when doing an HTTP request. This was useful for doing or another, and more obscure, HTTP request.

Note: Do not do this before confirming your server support commands.

The following options require a file description (obtained by using the fopen () function):

*curlopt_file: This file will be the output file you put in the transfer, the default is stdout.
*curlopt_infile: This file is the input file you sent over.
*curlopt_writeheader: This file is written with the head part of your output.
*curlopt_stderr: This file is written with errors rather than STDERR.

Several options to test the function:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <curl/curl.h>

#include <curl/easy.h>

Static size_t Save_header (void *ptr, size_t size, size_t nmemb, void *data)

{

Fwrite (PTR, size, nmemb, data);

return (size * nmemb);

}

int main (void)

{

Char url[] = "http://www.sina.com.cn/";

CURL *curl; Curlcode Res;

FILE *FP;

if (! ( fp = fopen ("htmheader.html", "W")))

{

printf ("fopen error\n");

return-1;

}

Curl_global_init (Curl_global_all);

Curl = Curl_easy_init ();

if (Curl)

{

Curl_easy_setopt (Curl, Curlopt_url, URL);

Curl_easy_setopt (Curl, Curlopt_nobody, 1L);

Curl_easy_setopt (Curl, Curlopt_header, 0L);

Curl_easy_setopt (Curl, curlopt_headerfunction, save_header);

Curl_easy_setopt (Curl, Curlopt_writeheader, FP);

Curl_easy_perform (curl);

}

Curl_easy_cleanup (curl);

Curl_global_cleanup ();

Fclose (FP);

return 0;

}

One of the key functions of the Curl_easy_setopt-curl Library

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.