PHP curl_setopt function
PHP curl_setopt function
(PHP 4> = 4.0.2, PHP 5)
curl_setopt - set a cURL transfer options.
Explanation
bool curl_setopt ( resource $ch , int $option , mixed $value )
Given cURL session handle set an option.
parameter
ch
By the curl_init () returns a cURL handle.
option
CURLOPT_XXX options need to be set.
value
Set the value of the option on the options.
For the following optional parameters of these option, value should be set to a value of type bool:
Options | Optional value value | Remark |
---|---|---|
CURLOPT_AUTOREFERER | When according Location: redirect automatically set header in the Referer: information. | |
CURLOPT_BINARYTRANSFER | CURLOPT_RETURNTRANSFER when enabled, return native (Raw) output. | |
CURLOPT_COOKIESESSION | When enabled curl will only pass a session cookie, ignore the other cookie, default status cURL will return all of the cookie to the server. session cookie are those used to determine the validity of the server-side session exists cookie. | |
CURLOPT_CRLF | When you enable the Unix newline into carriage return. | |
CURLOPT_DNS_USE_GLOBAL_CACHE | Enabled enabled a global DNS cache, enable this thread safe, and default. | |
CURLOPT_FAILONERROR | Display HTTP status code, the default behavior is to ignore the number of 400 or less HTTP information. | |
CURLOPT_FILETIME | It will try to modify remote information in the document is enabled. Results information is () CURLINFO_FILETIME option function returns through curl_getinfo. curl_getinfo (). | |
CURLOPT_FOLLOWLOCATION | When the server returns the server will enable the "Location:" header on the recursion back to the server, you can use CURLOPT_MAXREDIRS defined recursively return number. | |
CURLOPT_FORBID_REUSE | Forced disconnect after completion of interaction can not be reused. | |
CURLOPT_FRESH_CONNECT | Forced to get a new connection, replace the cache connection. | |
CURLOPT_FTP_USE_EPRT | When enabled FTP downloads, use EPRT (or LPRT) command. Disable EPRT and LPRT when set to FALSE, use the PORT command only. | |
CURLOPT_FTP_USE_EPSV | When enabled, the reply to the PASV mode FTP transfer process before the first try EPSV command. When set to FALSE to disable EPSV command. | |
CURLOPT_FTPAPPEND | Additional write file instead of overwriting it enabled. | |
CURLOPT_FTPASCII | CURLOPT_TRANSFERTEXT alias. | |
CURLOPT_FTPLISTONLY | List only the name of the FTP directory is enabled. | |
CURLOPT_HEADER | When you enable message header will be output as a data stream. | |
CURLINFO_HEADER_OUT | Tracking enabled handle request string. | Available since PHP 5.1.3. CURLINFO_ prefix is deliberate (intentional). |
CURLOPT_HTTPGET | The setting will enable the HTTP method is GET, since GET is the default, so use only in case of being modified. | |
CURLOPT_HTTPPROXYTUNNEL | It will be transmitted through the HTTP proxy is enabled. | |
CURLOPT_MUTE | The cURL function all modified parameters to restore the default value is enabled. | |
CURLOPT_NETRC | After the connection is established, access ~ / .netrc file for the user name and password information to connect remote sites. | |
CURLOPT_NOBODY | Will not be part of the HTML BODY output is enabled. | |
CURLOPT_NOPROGRESS | Close enabled curl transmission progress bar, this is enabled by default.
| |
CURLOPT_NOSIGNAL | Ignore all of the signal is passed to the php curl enabled performed. This is enabled by default when SAPI multithreaded transfer. | cURL 7.10 when added. |
CURLOPT_POST | When enabled sends a normal POST request, type: application / x-www-form -urlencoded, just like form submission. | |
CURLOPT_PUT | Allow HTTP send a file, you must set CURLOPT_INFILE and CURLOPT_INFILESIZE simultaneously enabled. | |
CURLOPT_RETURNTRANSFER | The curl_exec () returns the information obtained in the form of file stream, instead of being output. | |
CURLOPT_SSL_VERIFYPEER | After disabling cURL will be terminated from the server to verify. Use CURLOPT_CAINFO option certificates CURLOPT_CAPATH option certificates catalog If CURLOPT_SSL_VERIFYPEER (2 by default) is enabled, CURLOPT_SSL_VERIFYHOST needs to be set to TRUE, otherwise it is set to FALSE. | CURL 7.10 since the beginning of the default is TRUE. From the beginning of the default binding install cURL 7.10. |
CURLOPT_TRANSFERTEXT | Use ASCII mode for FTP transfers enabled. For LDAP, it retrieves the information in plain text rather than HTML. On Windows systems, the system will not set STDOUT to binary mode. | |
CURLOPT_UNRESTRICTED_AUTH | Using CURLOPT_FOLLOWLOCATION generated header multiple locations continuously append the user name and password information, even if the domain name has been changed. | |
CURLOPT_UPLOAD | When enabled allows file uploads. | |
CURLOPT_VERBOSE | Will report enabled all the information stored in STDERR or designated CURLOPT_STDERR in. |
For the following optional parameters of these option, value should be set to a value of integer type:
Options | Optional value value | Remark |
---|---|---|
CURLOPT_BUFFERSIZE | Each acquired data read into cache size, but does not guarantee that the value of each will be filled. | It was added in cURL 7.10. |
CURLOPT_CLOSEPOLICY | Not CURLCLOSEPOLICY_LEAST_RECENTLY_USED is CURLCLOSEPOLICY_OLDEST, there are three other CURLCLOSEPOLICY, but cURL is not yet supported. | |
CURLOPT_CONNECTTIMEOUT | Before initiating the connection waiting time, if set to 0, then wait indefinitely. | |
CURLOPT_CONNECTTIMEOUT_MS | Try to connect time to wait, in milliseconds. If set to 0, then wait indefinitely. | CURL 7.16.2 is added in. Available since PHP 5.2.3. |
CURLOPT_DNS_CACHE_TIMEOUT | Setting DNS information stored in the memory of the time, the default is 120 seconds. | |
CURLOPT_FTPSSLAUTH | FTP authentication: CURLFTPAUTH_SSL (first try SSL), CURLFTPAUTH_TLS (first attempt TLS) or CURLFTPAUTH_DEFAULT (let cURL automatically determined). | CURL 7.12.2 is added in. |
CURLOPT_HTTP_VERSION | CURL_HTTP_VERSION_NONE (default value, so cURL decide for themselves which version to use), CURL_HTTP_VERSION_1_0 (mandatory use of HTTP / 1.0) or CURL_HTTP_VERSION_1_1 (mandatory use of HTTP / 1.1). | |
CURLOPT_INFILESIZE | Set the upload file size limit of bytes (byte) units. | |
CURLOPT_LOW_SPEED_LIMIT | When the transmission rate is less than CURLOPT_LOW_SPEED_LIMIT time (bytes / sec), PHP based CURLOPT_LOW_SPEED_TIME to determine whether the transmission was canceled because of too slow. | |
CURLOPT_LOW_SPEED_TIME | When the transmission rate is less than CURLOPT_LOW_SPEED_LIMIT time (bytes / sec), PHP based CURLOPT_LOW_SPEED_TIME to determine whether the transmission was canceled because of too slow. | |
CURLOPT_MAXCONNECTS | The maximum number of connections to allow, through CURLOPT_CLOSEPOLICY will determine which connections should be stopped. | |
CURLOPT_MAXREDIRS | Specifies the maximum number of HTTP redirects, and CURLOPT_FOLLOWLOCATION This option is used together. | |
CURLOPT_PORT | It is used to specify the connection port. (Optional) | |
CURLOPT_PROTOCOLS | CURLPROTO_ * bit field means. If this is enabled, the bit field value will define which protocols can be used libcurl there during transmission. This will allow you to support a number of protocols when you compile libcurl, but limits except they be allowed to use a subset. The default libcurl will use all of its supported protocols. See CURLOPT_REDIR_PROTOCOLS protocol options available to: CURLPROTO_HTTP, CURLPROTO_HTTPS, CURLPROTO_FTP, CURLPROTO_FTPS , CURLPROTO_SCP, CURLPROTO_SFTP, CURLPROTO_TELNET, CURLPROTO_LDAP, CURLPROTO_LDAPS, CURLPROTO_DICT, CURLPROTO_FILE, CURLPROTO_TFTP, CURLPROTO_ALL | CURL 7.19.4 is added in. |
CURLOPT_PROTOCOLS | CURLPROTO_ * bit field means. If this is enabled, the bit field value will define which protocols can be used libcurl there during transmission. This will allow you to support a number of protocols when you compile libcurl, but limits except they be allowed to use a subset. The default libcurl will use all of its supported protocols. See CURLOPT_REDIR_PROTOCOLS protocol options available to: CURLPROTO_HTTP, CURLPROTO_HTTPS, CURLPROTO_FTP, CURLPROTO_FTPS , CURLPROTO_SCP, CURLPROTO_SFTP, CURLPROTO_TELNET, CURLPROTO_LDAP, CURLPROTO_LDAPS, CURLPROTO_DICT, CURLPROTO_FILE, CURLPROTO_TFTP, CURLPROTO_ALL | CURL 7.19.4 is added in. |
CURLOPT_PROXYAUTH | HTTP proxy authentication connection. Use CURLOPT_HTTPAUTH bitfield flag to set the appropriate options. For proxy authentication only CURLAUTH_BASIC and CURLAUTH_NTLM currently supported. | CURL 7.10.7 is added in. |
CURLOPT_PROXYPORT | Proxy server port. Ports can also be set in the CURLOPT_PROXY. | |
CURLOPT_PROXYTYPE | Not CURLPROXY_HTTP (default value) is CURLPROXY_SOCKS5. | It was added in cURL 7.10. |
CURLOPT_REDIR_PROTOCOLS | * CURLPROTO_ the bit field value. If this is enabled, the bit field value will limit the transmission protocol to follow when a redirector thread when CURLOPT_FOLLOWLOCATION turn can be used. This will redirect you to the limit transmission threads are allowed protocols subset default libcurl will allow all protocols except for FILE and SCP. This kind of pre-release version 7.19.4 and unconditionally follow all supported protocols there are some differences. About protocol constants, please refer CURLOPT_PROTOCOLS. | CURL 7.19.4 is added in. |
CURLOPT_RESUME_FROM | Pass a byte offset when restoring transmission (for HTTP). | |
CURLOPT_SSL_VERIFYHOST | Whether there is a common name (common name) 1 Check the server SSL certificate. Translator's Note: Common Name (Common Name) In general, you will want to apply is to fill SSL certificate domain (domain) or subdomain (sub domain). 2 Check the common name exists, and if it matches the hostname provided. | |
CURLOPT_SSLVERSION | Use the SSL version (2 or 3). By default PHP will detect this value themselves, although in some cases need to manually set. | |
CURLOPT_TIMECONDITION | If you are editing in the future at a specified time CURLOPT_TIMEVALUE too, use CURL_TIMECOND_IFMODSINCE return to the page, if not modified, and CURLOPT_HEADER is true, it returns a "304 Not Modified" in the header, CURLOPT_HEADER false, then use CURL_TIMECOND_IFUNMODSINCE, default value CURL_TIMECOND_IFUNMODSINCE. | |
CURLOPT_TIMEOUT | Set the maximum number of seconds to allow cURL executed. | |
CURLOPT_TIMEOUT_MS | Setting cURL allowed to execute the maximum number of milliseconds. | CURL 7.16.2 is added in. From PHP 5.2.3 onwards can be used. |
CURLOPT_TIMEVALUE | Setting a time stamp CURLOPT_TIMECONDITION used by default using CURL_TIMECOND_IFMODSINCE. |
For the following option of these optional parameters, value should be set to the value of a string type:
Options | Optional value value | Remark |
---|---|---|
CURLOPT_CAINFO | It holds one or a plurality of the server used to allow verification of the certificate file name. This parameter is only meaningful when used together and CURLOPT_SSL_VERIFYPEER. . | |
CURLOPT_CAPATH | Holds a plurality of CA certificates directory. This option is used together with CURLOPT_SSL_VERIFYPEER. | |
CURLOPT_COOKIE | Setting HTTP request "Cookie:" part of the contents. Multiple cookie separated by a semicolon with a space after the semicolon (eg, "fruit = apple; colour = red"). | |
CURLOPT_COOKIEFILE | File name contains the cookie data format cookie file can be in Netscape format, or just plain HTTP header information into a file. | |
CURLOPT_COOKIEJAR | Save cookie file information after the end of the connection. | |
CURLOPT_CUSTOMREQUEST | Use a custom request information instead of "GET" or "HEAD" as an HTTP request. This is the implementation of "DELETE" or other more obscure HTTP requests. Valid values such as "GET", "POST", "CONNECT" and so on. In other words, do not enter here the whole HTTP request. For example, enter "GET /index.html HTTP / 1.0 \ r \ n \ r \ n" is not correct.
| |
CURLOPT_EGDSOCKET | Similar CURLOPT_RANDOM_FILE, in addition to a Entropy Gathering Daemon socket. | |
CURLOPT_ENCODING | HTTP request header "Accept-Encoding:" value. Supported encoding "identity", "deflate", and "gzip". If an empty string "", the first request will be sent to all supported encoding types. | It was added in cURL 7.10. |
CURLOPT_FTPPORT | This value will be used to obtain the IP address for the FTP "POST" instruction needed. "POST" instruction tells the remote server to connect to our specified IP address. This string can be plain text IP address, hostname, a network interface name (UNIX under) or just a '-' to use the default IP address. | |
CURLOPT_INTERFACE | Net send interface name can be an interface name, IP address or a host name. | |
CURLOPT_KRB4LEVEL | KRB4 (Kerberos 4) security level. Any of the following values are valid (from low to high order): "clear", "safe ", "confidential", "private".. If the string does not match and these will be used "private". This option is set to NULL will disable KRB4 safety certification. Currently KRB4 safety certification can only be used for FTP transfers. | |
CURLOPT_POSTFIELDS | All data using the HTTP protocol in the "POST" operation to send. To send a file in front of the file name with the @ prefix and use the full path. This parameter can be similarly by urlencoded string after 'para1 = val1 & para2 = val2 & ...' or using a key field name, field data array values. If the value is an array, Content-Type header will be set to multipart / form-data. | |
CURLOPT_PROXY | HTTP proxy channel. | |
CURLOPT_PROXYUSERPWD | Format strings: one to connect to the proxy "[password] [username]" . | |
CURLOPT_RANDOM_FILE | SSL is used to generate a random number seed file name. | |
CURLOPT_RANGE | In "XY" form, wherein X and Y are both optional data acquisition range, in bytes. HTTP Transfer threads also supports several such intermediate duplicate entries separated by commas, such as "XY, NM". | |
CURLOPT_REFERER | Content: In the HTTP header "Referer" request. | |
CURLOPT_SSL_CIPHER_LIST | A list of SSL encryption algorithms. Such as RC4-SHA and TLSv1 are the list of available encryption. | |
CURLOPT_SSLCERT | The name of a file containing PEM format certificate. | |
CURLOPT_SSLCERTPASSWD | Using passwords CURLOPT_SSLCERT certificate required. | |
CURLOPT_SSLCERTTYPE | Type certificate. Supported formats are "PEM" (default value), "DER", and "ENG". | Joined in the cURL 7.9.3. |
CURLOPT_SSLENGINE | In CURLOPT_SSLKEY used to specify the SSL private key encryption engine variables. | |
CURLOPT_SSLENGINE_DEFAULT | Variables used for asymmetric cryptographic operations. | |
CURLOPT_SSLKEY | The file name contains SSL private key. | |
CURLOPT_SSLKEYPASSWD | CURLOPT_SSLKEY password specified in the SSL private key.
| |
CURLOPT_SSLKEYTYPE | Private key encryption types specified in CURLOPT_SSLKEY, supported key type is "PEM" (default value), "DER", and "ENG". | |
CURLOPT_URL | URL address need to get to be disposed curl_init () function. | |
CURLOPT_USERAGENT | In the HTTP request includes a "User-Agent:" header string. | |
CURLOPT_USERPWD | Passing a connection requires a user name and password in the format: "[username]: [password ]". |
These option for the following optional parameters, value should be set as an array:
Options | Optional value value | Remark |
---|---|---|
CURLOPT_HTTP200ALIASES | 200 response code array, the array of response it is considered the correct response, otherwise it is considered an error. | CURL 7.10.3 is added in. |
CURLOPT_HTTPHEADER | To set a array of HTTP header fields. Use an array of the following settings: array ( 'Content-type: text / plain', 'Content-length: 100') | |
CURLOPT_POSTQUOTE | After the execution is complete FTP requests on the server to perform a set of FTP commands. | |
CURLOPT_QUOTE | A set of FTP commands executed on the server prior to the FTP request. |
For the following optional parameters of these option, value should be set to a flow of resources (for example, using fopen ()):
Options | Optional value value |
---|---|
CURLOPT_FILE | Set output file location, value is a resource type, the default is STDOUT (the browser). |
CURLOPT_INFILE | Address file when uploading a file to be read, the value of a resource type. |
CURLOPT_STDERR | Set an error output address value is a resource type, instead of the default STDERR. |
CURLOPT_WRITEHEADER | Setting write header part of the content of the address file, value is a resource type. |
For the following option of these optional parameters, value should be set to a callback function name:
Options | Optional value value |
---|---|
CURLOPT_HEADERFUNCTION | Set up a callback function that takes two arguments, the first is the cURL resource handle, and the second header data is output. Output header data must depend on the function that returns the size of the data has been written. |
CURLOPT_PASSWDFUNCTION | Setting a callback function takes three arguments, the first is the cURL resource handle, and the second is a password prompt, the third parameter is the maximum password length allowed. Returns the value of the password. |
CURLOPT_PROGRESSFUNCTION | Setting a callback function takes three arguments, the first is the cURL resource handle, and the second is a resource file descriptors, and the third is the length. It returns the data it contains. |
CURLOPT_READFUNCTION | Callback function name. This function takes three parameters should be. The first is the cURL resource; the second is the option to pass cURL CURLOPT_INFILE the stream resource; third parameter is the maximum number that can be read data. The callback function must return a string length less than or equal the amount of data requested (third parameter). General read from the incoming stream resource. Returns an empty string as EOF (end of file) signals. |
CURLOPT_WRITEFUNCTION | Callback function name. The function should accept two arguments. The first is the cURL resource; the second is a string of data to be written. Data must be stored in the function. The function must return the exact number of bytes to be written to the incoming data, otherwise the transmission will be interrupted by an error. |
return value
Successful return TRUE, or on failure returns FALSE.
Update Log
version | Explanation |
---|---|
5.2.10 | Introducing CURLOPT_PROTOCOLS , and CURLOPT_REDIR_PROTOCOLS . |
5.1.0 | Introducing CURLOPT_AUTOREFERER , CURLOPT_BINARYTRANSFER , CURLOPT_FTPSSLAUTH , CURLOPT_PROXYAUTH , and CURLOPT_TIMECONDITION . |
5.0.0 | Introducing CURLOPT_FTP_USE_EPRT , CURLOPT_NOSIGNAL , CURLOPT_UNRESTRICTED_AUTH , CURLOPT_BUFFERSIZE , CURLOPT_HTTPAUTH , CURLOPT_PROXYPORT , CURLOPT_PROXYTYPE , CURLOPT_SSLCERTTYPE , and CURLOPT_HTTP200ALIASES . |
Examples
Initializes a new cURL session and obtain a web page
<?php // 创建一个新cURL资源 $ch = curl_init(); // 设置URL和相应的选项 curl_setopt($ch, CURLOPT_URL, "http://www.example.com/"); curl_setopt($ch, CURLOPT_HEADER, false); // 抓取URL并把它传递给浏览器 curl_exec($ch); //关闭cURL资源,并且释放系统资源 curl_close($ch); ?>
Upload file Example:
<?php /* http://localhost/upload.php: print_r($_POST); print_r($_FILES); */ $ch = curl_init(); $data = array('name' => 'Foo', 'file' => '@/home/user/test.png'); curl_setopt($ch, CURLOPT_URL, 'http://localhost/upload.php'); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_exec($ch); ?>
Examples of the above output results are as follows:
Array ( [name] => Foo ) Array ( [file] => Array ( [name] => test.png [type] => image/png [tmp_name] => /tmp/phpcpjNeQ [error] => 0 [size] => 279 ) )
Note
Passing an array CURLOPT_POSTFIELDS, cURL data will be encoded into multipart / form-data, and then pass a URL-encoded string, the data is encoded as application / x-www-form-urlencoded.