PHP curl_setopt Function
(PHP 4 >= 4.0.2, PHP 5)
curl_setopt — Set an option for a cURL transfer.
Description
bool curl_setopt ( resource $ch , int $option , mixed $value )
Sets an option on the given cURL session handle.
Parameters
** ch**
A cURL handle returned by curl_init().
** option **
The CURLOPT_XXX option to set.
value
The value to set on the option.
For the following options, value should be set to a bool:
| Option | OptionalvalueValue | Notes |
|---|---|---|
| CURLOPT_AUTOREFERER | When following a *Location:redirect, automatically set theReferer:* information in the header. | |
| CURLOPT_BINARYTRANSFER | WhenCURLOPT_RETURNTRANSFERis enabled, returns the raw output. | |
| CURLOPT_COOKIESESSION | When enabled, cURL will only pass a session cookie and ignore other cookies; by default cURL sends all cookies back to the server. A session cookie is one that exists to determine whether the server-side session is valid. | |
| CURLOPT_CRLF | When enabled, converts Unix newlines to carriage return + newline. | |
| CURLOPT_DNS_USE_GLOBAL_CACHE | When enabled, enables a global DNS cache; this is thread-safe and enabled by default. | |
| CURLOPT_FAILONERROR | Displays the HTTP status code; by default, HTTP information with a code less than or equal to 400 is ignored. | |
| CURLOPT_FILETIME | When enabled, tries to modify the information in the remote document. The resulting information is returned via theCURLINFO_FILETIMEoption of the curl_getinfo() function. | |
| CURLOPT_FOLLOWLOCATION | When enabled, recursively sends the *"Location: "* returned by the server back to the server in the header; useCURLOPT_MAXREDIRSto limit the number of recursive redirects. | |
| CURLOPT_FORBID_REUSE | Forces the connection to close after the interaction is complete, so it cannot be reused. | |
| CURLOPT_FRESH_CONNECT | Forces a new connection instead of using a cached one. | |
| CURLOPT_FTP_USE_EPRT | When enabled, uses the EPRT (or LPRT) command for FTP downloads. Set toFALSEto disable EPRT and LPRT and use the PORT command only. | |
| CURLOPT_FTP_USE_EPSV | When enabled, tries the EPSV command first before falling back to PASV mode during FTP transfers. Set toFALSEto disable the EPSV command. | |
| CURLOPT_FTPAPPEND | When enabled, appends to the file instead of overwriting it. | |
| CURLOPT_FTPASCII | CURLOPT_TRANSFERTEXTis an alias of. | |
| CURLOPT_FTPLISTONLY | When enabled, lists only the names in an FTP directory. | |
| CURLOPT_HEADER | When enabled, outputs header information as part of the data stream. | |
| CURLINFO_HEADER_OUT | When enabled, traces the handle's request string. | Available since PHP 5.1.3. The **CURLINFO_** prefix is intentional. |
| CURLOPT_HTTPGET | When enabled, sets the HTTP method to GET; since GET is the default, this is only used when it has been changed. | |
| CURLOPT_HTTPPROXYTUNNEL | When enabled, transfers through an HTTP proxy. | |
| CURLOPT_MUTE | When enabled, resets all modified parameters in the cURL functions to their default values. | |
| CURLOPT_NETRC | After the connection is established, access the~/.netrcfile to obtain the username and password used to connect to the remote site. | |
| CURLOPT_NOBODY | When enabled, the BODY part of the HTML is not output. | |
| CURLOPT_NOPROGRESS |
When enabled, disables the progress bar for cURL transfers; this option is enabled by default. |
Note:
PHP automatically sets this option toTRUE; this option should only be changed for debugging purposes.
| | | **CURLOPT_NOSIGNAL** | When enabled, ignores all signals passed by cURL to PHP. Enabled by default in SAPI multi-threaded transfers. | Added in cURL 7.10. | | **CURLOPT_POST** | When enabled, sends a regular POST request of type *application/x-www-form-urlencoded*, just like a form submission. | | | **CURLOPT_PUT** | When enabled, allows HTTP file uploads; **CURLOPT_INFILE** and **CURLOPT_INFILESIZE** must also be set. | | | **CURLOPT_RETURNTRANSFER** | Returns the information fetched by curl_exec() as a string instead of outputting it directly. | | | **CURLOPT_SSL_VERIFYPEER** | When disabled, cURL stops verifying the peer. Use **CURLOPT_CAINFO** to set the certificate and **CURLOPT_CAPATH** to set the certificate directory. If **CURLOPT_SSL_VERIFYPEER** (default value 2) is enabled, **CURLOPT_SSL_VERIFYHOST** must be set to **TRUE**, otherwise set it to **FALSE**. | Defaults to **TRUE** since cURL 7.10; bundled by default since cURL 7.10. | | **CURLOPT_TRANSFERTEXT** | When enabled, uses ASCII mode for FTP transfers. For LDAP, it retrieves plain text instead of HTML. On Windows systems, *STDOUT* is not set to binary mode. | | | **CURLOPT_UNRESTRICTED_AUTH** | Keeps appending username and password information across multiple locations in the header produced by **CURLOPT_FOLLOWLOCATION**, even if the domain name changes. | | | **CURLOPT_UPLOAD** | When enabled, allows file uploads. | | | **CURLOPT_VERBOSE** | When enabled, reports all information, stored in *STDERR* or the specified **CURLOPT_STDERR**. | |For the following options, value should be set to an integer:
| Option | OptionalvalueValue | Notes |
|---|---|---|
| CURLOPT_BUFFERSIZE | The size of the buffer read for each fetch, although this value is not guaranteed to be filled every time. | Added in cURL 7.10. |
| CURLOPT_CLOSEPOLICY | Either CURLCLOSEPOLICY_LEAST_RECENTLY_USED or CURLCLOSEPOLICY_OLDEST; there are three other CURLCLOSEPOLICY constants, but cURL does not support them yet. | |
| CURLOPT_CONNECTTIMEOUT | The time to wait before initiating a connection; if set to 0, waits indefinitely. | |
| CURLOPT_CONNECTTIMEOUT_MS | The time to wait while attempting to connect, in milliseconds. If set to 0, waits indefinitely. | Added in cURL 7.16.2. Available since PHP 5.2.3. |
| CURLOPT_DNS_CACHE_TIMEOUT | Sets how long DNS information is kept in memory; the default is 120 seconds. | |
| CURLOPT_FTPSSLAUTH | FTP authentication method:CURLFTPAUTH_SSL (try SSL first),CURLFTPAUTH_TLS (try TLS first), orCURLFTPAUTH_DEFAULT (let cURL decide automatically). | Added in cURL 7.12.2. |
| CURLOPT_HTTP_VERSION | CURL_HTTP_VERSION_NONE (default, lets cURL decide which version to use),CURL_HTTP_VERSION_1_0 (force HTTP/1.0), orCURL_HTTP_VERSION_1_1 (force HTTP/1.1). | |
| CURLOPT_INFILESIZE | Sets the size limit of the uploaded file, in bytes. | |
| CURLOPT_LOW_SPEED_LIMIT | When the transfer speed is less thanCURLOPT_LOW_SPEED_LIMIT(bytes/sec), PHP usesCURLOPT_LOW_SPEED_TIMEto decide whether to cancel the transfer for being too slow. | |
| CURLOPT_LOW_SPEED_TIME | When the transfer speed is less thanCURLOPT_LOW_SPEED_LIMIT(bytes/sec), PHP usesCURLOPT_LOW_SPEED_TIMEto decide whether to cancel the transfer for being too slow. | |
| CURLOPT_MAXCONNECTS | The maximum number of connections allowed; when exceeded,CURLOPT_CLOSEPOLICYdetermines which connections should be stopped. | |
| CURLOPT_MAXREDIRS | Specifies the maximum number of HTTP redirects; this option is used together withCURLOPT_FOLLOWLOCATION. | |
| CURLOPT_PORT | Used to specify the connection port. (Optional) | |
| CURLOPT_PROTOCOLS | *CURLPROTO_A bitmask. When enabled, the bitmask limits which protocols libcurl may use during a transfer. This allows you to compile libcurl with support for many protocols but restrict it to an allowed subset at runtime. By default libcurl uses all protocols it supports. SeeCURLOPT_REDIR_PROTOCOLS. Available protocol options are: 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 | Added in cURL 7.19.4. |
| CURLOPT_PROTOCOLS | *CURLPROTO_A bitmask. When enabled, the bitmask limits which protocols libcurl may use during a transfer. This allows you to compile libcurl with support for many protocols but restrict it to an allowed subset at runtime. By default libcurl uses all protocols it supports. SeeCURLOPT_REDIR_PROTOCOLS. Available protocol options are: 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 | Added in cURL 7.19.4. |
| CURLOPT_PROXYAUTH | The authentication method for HTTP proxy connections. Use the bitmask flags inCURLOPT_HTTPAUTHto set the corresponding options. For proxy authentication, onlyCURLAUTH_BASICandCURLAUTH_NTLMis currently supported. | Added in cURL 7.10.7. |
| CURLOPT_PROXYPORT | The port of the proxy server. The port can also be set inCURLOPT_PROXY. | |
| CURLOPT_PROXYTYPE | EitherCURLPROXY_HTTP (the default) orCURLPROXY_SOCKS5. | Added in cURL 7.10. |
| CURLOPT_REDIR_PROTOCOLS | CURLPROTO_*A bitmask. When enabled, the bitmask limits which protocols the transfer may follow whenCURLOPT_FOLLOWLOCATIONis enabled and a redirect is followed. This lets you restrict the transfer to an allowed subset of protocols on redirect; by default libcurl allows all protocols except FILE and SCP. This differs somewhat from the 7.19.4 pre-release, which unconditionally followed all supported protocols. For protocol constants, seeCURLOPT_PROTOCOLS. | Added in cURL 7.19.4. |
| CURLOPT_RESUME_FROM | Passes a byte offset when resuming a transfer (for resumable downloads). | |
| CURLOPT_SSL_VERIFYHOST | 1 Checks whether a common name exists in the server's SSL certificate. Translator's note: the Common Name is generally the domain (or sub-domain) you enter when applying for an SSL certificate. 2 Checks that the common name exists and matches the host name provided. | |
| CURLOPT_SSLVERSION | The SSL version to use (2 or 3). By default PHP detects this value itself, although in some cases it needs to be set manually. | |
| CURLOPT_TIMECONDITION | If the file has been modified after the time specified inCURLOPT_TIMEVALUE, then useCURL_TIMECOND_IFMODSINCEto return the page; if it has not been modified andCURLOPT_HEADERis true, a *"304 Not Modified"*header is returned; CURLOPT_HEADERif it is false, useCURL_TIMECOND_IFUNMODSINCE*. The default isCURL_TIMECOND_IFUNMODSINCE. | |
| CURLOPT_TIMEOUT | Sets the maximum number of seconds cURL is allowed to execute. | |
| CURLOPT_TIMEOUT_MS | Sets the maximum number of milliseconds cURL is allowed to execute. | Added in cURL 7.16.2. Available since PHP 5.2.3. |
| CURLOPT_TIMEVALUE | Sets aCURLOPT_TIMECONDITIONtimestamp to use; by default,CURL_TIMECOND_IFMODSINCE. |
For the following options, value should be set to a string:
| Option | OptionalvalueValue | Notes |
|---|---|---|
| CURLOPT_CAINFO | The name of a file holding one or more certificates used to verify the peer. This parameter is only meaningful when used together withCURLOPT_SSL_VERIFYPEER. | |
| CURLOPT_CAPATH | A directory holding multiple CA certificates. This option is used together withCURLOPT_SSL_VERIFYPEER. | |
| CURLOPT_COOKIE | Sets the content of the *"Cookie: "* part of the HTTP request. Multiple cookies are separated by a semicolon followed by a space (for example, "fruit=apple; colour=red"). | |
| CURLOPT_COOKIEFILE | The name of a file containing cookie data. The cookie file can be in Netscape format, or simply plain HTTP header information saved to a file. | |
| CURLOPT_COOKIEJAR | A file in which to save cookie information after the connection ends. | |
| CURLOPT_CUSTOMREQUEST |
Use a custom request method instead of"GET"or"HEAD"as the HTTP request. This is useful for performing"DELETE" or other less common HTTP requests. Valid values include"GET", "POST", "CONNECT"and so on. In other words, do not enter the entire HTTP request here. For example, entering"GET /index.html HTTP/1.0\r\n\r\n"is incorrect. |
Note:
Do not use this until you are sure the server supports the custom request method.
| | | **CURLOPT_EGDSOCKET** | Like **CURLOPT_RANDOM_FILE**, except for an Entropy Gathering Daemon socket. | | | **CURLOPT_ENCODING** | The value of *"Accept-Encoding: "* in the HTTP request header. Supported encodings are *"identity"*, *"deflate"*, and *"gzip"*. If an empty string *""* is used, the header sends all supported encoding types. | Added in cURL 7.10. | | **CURLOPT_FTPPORT** | This value is used to obtain the IP address required by the FTP "POST" command. The "POST" command tells the remote server to connect to the IP address we specify. This string can be a plain-text IP address, a host name, a network interface name (on UNIX), or just a '-' to use the default IP address. | | | **CURLOPT_INTERFACE** | The name of the outgoing network interface; it can be an interface name, an IP address, or a host name. | | | **CURLOPT_KRB4LEVEL** | KRB4 (Kerberos 4) security level. Any of the following values is valid (from lowest to highest): *"clear"*, *"safe"*, *"confidential"*, *"private".* If the string matches none of these, *"private"* is used. Setting this option to **NULL** disables KRB4 security authentication. Currently KRB4 security authentication can only be used for FTP transfers. | | | **CURLOPT_POSTFIELDS** | All data is sent using the "POST" operation in the HTTP protocol. To send a file, prefix the file name with *@* and use the full path. This parameter can be passed as a urlencoded string such as '*para1=val1¶2=val2&...*' or as an array with field names as keys and field data as values. If *value* is an array, the *Content-Type* header is set to *multipart/form-data*. | | | **CURLOPT_PROXY** | The HTTP proxy tunnel. | | | **CURLOPT_PROXYUSERPWD** | A string in the format *"[username]:[password]"* used to connect to the proxy. | | | **CURLOPT_RANDOM_FILE** | The name of a file used to generate the SSL random seed. | | | **CURLOPT_RANGE** | A range of data to retrieve in the form *"X-Y"*, where X and Y are both optional, in bytes. HTTP transfers also support several such ranges separated by commas, such as *"X-Y,N-M"*. | | | **CURLOPT_REFERER** | The content of *"Referer: "* in the HTTP request header. | | | **CURLOPT_SSL_CIPHER_LIST** | A list of SSL cipher algorithms. For example, *RC4-SHA* and *TLSv1* are available cipher lists. | | | **CURLOPT_SSLCERT** | The name of a file containing a PEM-formatted certificate. | | | **CURLOPT_SSLCERTPASSWD** | The password required for the certificate used by **CURLOPT_SSLCERT**. | | | **CURLOPT_SSLCERTTYPE** | The type of the certificate. Supported formats are *"PEM"* (default), *"DER"*, and *"ENG"*. | Added in cURL 7.9.3. | | **CURLOPT_SSLENGINE** | The crypto engine variable for the SSL private key specified in **CURLOPT_SSLKEY**. | | | **CURLOPT_SSLENGINE_DEFAULT** | The variable used for asymmetric cryptographic operations. | | | **CURLOPT_SSLKEY** | The name of the file containing the SSL private key. | | | **CURLOPT_SSLKEYPASSWD** |inCURLOPT_SSLKEYThe password for the SSL private key specified in
| | | **CURLOPT_SSLKEYTYPE** | The encryption type of the private key specified in **CURLOPT_SSLKEY**; supported key types are *"PEM"* (default), *"DER"*, and *"ENG"*. | | | **CURLOPT_URL** | The URL to fetch; this can also be set in the curl_init() function. | | | **CURLOPT_USERAGENT** | A string containing a *"User-Agent: "* header in the HTTP request. | | | **CURLOPT_USERPWD** | A username and password required for the connection, in the format *"[username]:[password]"*. | |Note:
Since this option contains sensitive password information, remember to keep the PHP script secure.
For the following options, value should be set to an array:
| Option | OptionalvalueValue | Notes |
|---|---|---|
| CURLOPT_HTTP200ALIASES | An array of 200 response codes; the response codes in the array are considered valid, otherwise they are considered errors. | Added in cURL 7.10.3. |
| CURLOPT_HTTPHEADER | An array used to set HTTP header fields, in the following form: array('Content-type: text/plain', 'Content-length: 100') | |
| CURLOPT_POSTQUOTE | An array of FTP commands to execute on the server after the FTP request has been performed. | |
| CURLOPT_QUOTE | An array of FTP commands to execute on the server before the FTP request. |
For the following options, value should be set to a stream resource (for example using fopen()):
| Option | OptionalvalueValue |
|---|---|
| CURLOPT_FILE | Sets the output file location; the value is a resource, defaulting toSTDOUT (the browser). |
| CURLOPT_INFILE | The file to read when uploading a file; the value is a resource. |
| CURLOPT_STDERR | Sets an error output location; the value is a resource that replaces the defaultSTDERR. |
| CURLOPT_WRITEHEADER | Sets the file to which the header section is written; the value is a resource. |
For the following options, value should be set to a callback function name:
| Option | OptionalvalueValue |
|---|---|
| CURLOPT_HEADERFUNCTION | Sets a callback function with two parameters: the first is the cURL resource handle, and the second is the output header data. Output of the header data must be handled by this function, which returns the number of bytes written. |
| CURLOPT_PASSWDFUNCTION | Sets a callback function with three parameters: the first is the cURL resource handle, the second is a password prompt, and the third is the maximum allowed password length. It returns the password value. |
| CURLOPT_PROGRESSFUNCTION | Sets a callback function with three parameters: the first is the cURL resource handle, the second is a file descriptor resource, and the third is the length. It returns the data contained. |
| CURLOPT_READFUNCTION | A callback function name. The function should accept three parameters. The first is a cURL resource; the second is the stream resource passed to cURL via the option CURLOPT_INFILE ; the third is the maximum amount of data that can be read. The callback must return a string whose length is less than or equal to the requested amount (the third parameter). It normally reads from the passed stream resource. An empty string is returned as the EOF(end-of-file) signal. |
| CURLOPT_WRITEFUNCTION | A callback function name. The function should accept two parameters. The first is a cURL resource; the second is the data string to be written. The data must be saved by the function. The function must return the exact number of bytes of the passed data, otherwise the transfer is aborted with an error. |
Return Value
Returns TRUE on success, or FALSE on failure.
Changelog
| Version | Description |
|---|---|
| 5.2.10 | Introduced in CURLOPT_PROTOCOLS, and CURLOPT_REDIR_PROTOCOLS. |
| 5.1.0 | Introduced in CURLOPT_AUTOREFERER, CURLOPT_BINARYTRANSFER, CURLOPT_FTPSSLAUTH, CURLOPT_PROXYAUTH, and CURLOPT_TIMECONDITION. |
| 5.0.0 | Introduced in CURLOPT_FTP_USE_EPRT, CURLOPT_NOSIGNAL, CURLOPT_UNRESTRICTED_AUTH, CURLOPT_BUFFERSIZE, CURLOPT_HTTPAUTH, CURLOPT_PROXYPORT, CURLOPT_PROXYTYPE, CURLOPT_SSLCERTTYPE, and CURLOPT_HTTP200ALIASES. |
Example
Initialize a new cURL session and fetch a web page
File upload example:
'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);
?>
The output of the above example is:
Array
(
[name] => Foo
)
Array
(
[file] => Array
(
[name] => test.png
[type] => image/png
[tmp_name] => /tmp/phpcpjNeQ
[error] => 0
[size] => 279
)
)
Notes
Passing an array to CURLOPT_POSTFIELDS makes cURL encode the data as multipart/form-data, whereas passing a URL-encoded string encodes the data as application/x-www-form-urlencoded.
PHP cURL Reference Manual