PHPCURL-php Tutorial

Source: Internet
Author: User
PHPCURL description 1. CURL is an open source file transmission tool that uses the URL syntax in the command line mode.


2. it is widely used in Unix and multiple Linux distributions, and has DOS, Win32, and Win64 versions.


3. it supports many protocols: FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE, and LDAP.


4. CURL is developed by CURLl in Sweden and you can access: http://curl.haxx. se to get its source code and related instructions.


5. php curl function list:

Function

Description

Curl_close ()

Closes a cURL session.

Curl_copy_handle ()

Copy a cURL handle and all its options.

Curl_errno ()

Returns the last error code.

Curl_error ()

Returns a string that protects the current session from the last error.

Curl_escape ()

Returns an escape string, which is URL encoded.

Curl_exec ()

Execute a cURL session.

Curl_file_create ()

Create a CURLFile object.

Curl_getinfo ()

Obtains the information of a cURL connection resource handle.

Curl_init ()

Initialize a cURL session.

Curl_multi_add_handle ()

Add a separate curl handle to the curl batch processing session.

Curl_multi_close ()

Closes a group of cURL handles.

Curl_multi_exec ()

Run the subconnection of the current cURL handle.

Curl_multi_getcontent ()

If CURLOPT_RETURNTRANSFER is set, the obtained text stream is returned.

Curl_multi_info_read ()

Obtain the transmission information of the currently resolved cURL.

Curl_multi_init ()

Returns a new cURL batch handle.

Curl_multi_remove_handle ()

Removes a handle from a curl batch processing resource.

Curl_multi_select ()

Wait for the active connection in all cURL batch processing.

Curl_multi_setopt ()

Sets a batch cURL transfer option.

Curl_multi_strerror ()

Returns the string text that describes the error code.

Curl_pause ()

Pause and resume the connection.

Curl_reset ()

Reset all options of the libcurl session handle.

Curl_setopt_array ()

Set the option for cURL transmission sessions in batches.

Curl_setopt ()

Sets a cURL transfer option.

Curl_pai_close ()

Disable the cURL sharing handle.

Curl_cmd_init ()

Initialize the cURL sharing handle.

Curl_pai_setopt ()

Sets the cURL transfer option for a shared handle.

Curl_strerror ()

Returns the string description of the error code.

Curl_unescape ()

Decodes the URL encoded string.

Curl_version ()

Obtain the cURL version.


6. basic steps for CURL development

(1) Initialization: curl_init ()

(2) set the variable: curl_setopt () or curl_setopt_array ()

(3) execute and obtain the result: curl_exec ()

(4) release CURL handle: curl_close ()

7. Summary of CURLOPT parameters:

CURLOPT_AUTOREFERER
Automatically set referer information in the header

CURLOPT_BINARYTRANSFER
When CURLOPT_RETURNTRANSFER is enabled, data is returned.

CURLOPT_COOKIESESSION
When curl is enabled, only one session cookie is passed and other cookies are ignored. by default, curl returns all cookies to the server. Session cookie is a cookie used to determine whether a session on the server is valid.

CURLOPT_CRLF
When it is enabled, the Unix line break is converted into a carriage return line break.

CURLOPT_DNS_USE_GLOBAL_CACHE
When enabled, a global DNS cache is enabled. this option is thread-safe and the default value is true.

CURLOPT_FAILONERROR
The HTTP status code is displayed. the default behavior is to ignore the HTTP information whose number is less than or equal to 400.

CURLOPT_FILETIME
When enabled, the system tries to modify the information in the remote document. The result is returned through the CURLINFO_FILETIME option of the curl_getinfo () function.

CURLOPT_FOLLOWLOCATION
When enabled, the "Location:" returned by the server is placed in the header and recursively returned to the server. you can use CURLOPT_MAXREDIRS to limit the number of recursive responses.

CURLOPT_FORBID_REUSE
After the interaction is completed, the connection is forcibly disconnected and cannot be reused.

CURLOPT_FRESH_CONNECT
Force a new connection to replace the connection in the cache.

CURLOPT_FTP_USE_EPRT

CURLOPT_FTP_USE_EPSV

CURLOPT_FTPAPPEND

CURLOPT_FTPASCII

CURLOPT_FTPLISTONLY

CURLOPT_HEADER
When enabled, the header file information is output as a data stream.

CURLOPT_HTTPGET
When enabled, the HTTP method is set to GET. because GET is set by default, it is used only when it is modified.

CURLOPT_HTTPPROXYTUNNEL
It is transmitted through the HTTP proxy when it is enabled.

CURLOPT_MUTE
This section describes how to restore the default values of all modified parameters in the curl function.

CURLOPT_NETRC
After the connection is established, access ~ /. The netrc file obtains the user name and password to connect to the remote site.

CURLOPT_NOBODY
When enabled, the HTML body section is not output.

CURLOPT_NOPROGRESS
Disable the curl transfer progress bar when enabling. the default value of this item is true.

CURLOPT_NOSIGNAL
When enabled, ignore all curl signals sent to php. This option is enabled by default during SAPI multi-thread transmission.

CURLOPT_POST
When it is enabled, a conventional POST request is sent. the type is application/x-www-form-urlencoded, just like form submission.

CURLOPT_PUT
When enabled, files can be sent over HTTP. both CURLOPT_INFILE and CURLOPT_INFILESIZE must be set.

CURLOPT_RETURNTRANSFER
The information obtained by curl_exec () is returned in the form of a file stream, rather than directly output.

CURLOPT_SSL_VERIFYPEER

CURLOPT_TRANSFERTEXT

CURLOPT_UNRESTRICTED_AUTH
The username and password information is continuously appended to multiple locations in the header generated by CURLOPT_FOLLOWLOCATION, even if the domain name has changed.

CURLOPT_UPLOAD
Enable file transfer

CURLOPT_VERBOSE
When enabled, all information is reported and stored in STDERR or the specified CURLOPT_STDERR.

CURLOPT_BUFFERSIZE
The size of the data read into the cache each time. This value is filled every time.

CURLOPT_CLOSEPOLICY
Either CURLCLOSEPOLICY_LEAST_RECENTLY_USED or CURLCLOSEPOLICY_OLDEST exists, but curl is not supported yet ..

CURLOPT_CONNECTTIMEOUT
The waiting time before the connection is initiated. if it is set to 0, no waiting is performed.

CURLOPT_DNS_CACHE_TIMEOUT
Set the time for saving DNS information in the memory. the default value is 120 seconds.

CURLOPT_FTPSSLAUTH

CURLOPT_HTTP_VERSION
Set the HTTP protocol used by curl, CURL_HTTP_VERSION_NONE (let curl determine by itself), CURL_HTTP_VERSION_1_0 (HTTP/1.0), CURL_HTTP_VERSION_1_1 (HTTP/1.1)

CURLOPT_HTTPAUTH
The HTTP verification method used. optional values include CURLAUTH_BASIC, CURLAUTH_DIGEST, CURLAUTH_GSSNEGOTIATE, CURLAUTH_NTLM, CURLAUTH_ANY, and CURLAUTH_ANYSAFE. multiple values can be separated by the "|" operator, curl allows the server to select the best value. CURLAUTH_ANY is equivalent to CURLAUTH_BASIC | CURLAUTH_DIGEST | digest | CURLAUTH_NTLM, CURLAUTH_ANYSAFE is equivalent to CURLAUTH_DIGEST | digest | CURLAUTH_NTLM

CURLOPT_INFILESIZE
Set the size of the uploaded file

CURLOPT_LOW_SPEED_LIMIT
When the transmission speed is less than CURLOPT_LOW_SPEED_LIMIT, PHP will determine whether to cancel the transmission because it is too slow based on CURLOPT_LOW_SPEED_TIME.

CURLOPT_LOW_SPEED_TIME
The number of seconds the transfer shocould be below CURLOPT_LOW_SPEED_LIMIT for PHP to consider the transfer too slow and abort.
When the transmission speed is less than CURLOPT_LOW_SPEED_LIMIT, PHP will determine whether to cancel the transmission because it is too slow based on CURLOPT_LOW_SPEED_TIME.

CURLOPT_MAXCONNECTS
The maximum number of connections allowed. if the maximum number is exceeded, the CURLOPT_CLOSEPOLICY is used to determine which connections should be stopped.

CURLOPT_MAXREDIRS
Specifies the maximum number of HTTP redirects. this option is used with CURLOPT_FOLLOWLOCATION.

CURLOPT_PORT
An optional parameter used to specify the number of connection ports

CURLOPT_PROXYAUTH

CURLOPT_PROXYPORT

CURLOPT_PROXYTYPE

CURLOPT_RESUME_FROM
Transmit a byte offset when resuming transmission (used for resumable transmission)

CURLOPT_SSL_VERIFYHOST

CURLOPT_SSLVERSION

CURLOPT_TIMECONDITION
If it has been edited after a certain time specified by CURLOPT_TIMEVALUE, CURL_TIMECOND_IFMODSINCE is used to return to the page. if it has Not been Modified and CURLOPT_HEADER is true, a header of "304 Not Modified" is returned, if CURLOPT_HEADER is false, use CURL_TIMECOND_ISUNMODSINCE. the default value is CURL_TIMECOND_IFMODSINCE.

CURLOPT_TIMEOUT
Sets the maximum number of seconds that curl can be executed.

CURLOPT_TIMEVALUE
Set a timestamp used by CURLOPT_TIMECONDITION. by default, CURL_TIMECOND_IFMODSINCE is used.

CURLOPT_CAINFO

CURLOPT_CAPATH

CURLOPT_COOKIE
Set the content in the "Set-Cookie:" section of the HTTP request.

CURLOPT_COOKIEFILE
The name of the file containing the cookie information. the cookie file can be in Netscape format or HTTP header information.

CURLOPT_COOKIEJAR
Name of the file that stores cookie information after the connection is closed

CURLOPT_CUSTOMREQUEST

CURLOPT_EGBSOCKET

CURLOPT_ENCODING
The content in "Accept-Encoding:" in the header. the supported Encoding formats are: "identity", "deflate", and "gzip ". If it is set to an empty string, all encoding formats are supported.

CURLOPT_FTPPORT

CURLOPT_INTERFACE
The name used in the external network interface can be an interface name, IP address or host name.

CURLOPT_KRB4LEVEL
KRB4 (Kerberos 4) security level settings can be one of the following values: "clear", "safe", "confidential", "private ". The default value is "private". if it is set to null, KRB4 is disabled. Currently, KRB4 can only be used for FTP transmission.

CURLOPT_POSTFIELDS
The "POST" operation in HTTP. If you want to transfer a file, you need a file name starting @.

CURLOPT_PROXY
Set the HTTP proxy server

CURLOPT_PROXYUSERPWD
The username and password used to connect to the proxy server in the format of [username]: [password.

CURLOPT_RANDOM_FILE
Set the name of the file that stores the random seed used by SSL.

CURLOPT_RANGE
Sets an HTTP Transport range, which can be set as a X-Y, and if there are multiple HTTP transport, separate multiple values, such as: "X-Y, n-M ".

CURLOPT_REFERER
Set the value of "Referer:" in the header.

CURLOPT_SSL_CIPHER_LIST

CURLOPT_SSLCERT
Pass a string that contains a PEM certificate.

CURLOPT_SSLCERTPASSWD
Pass a password that contains the password required to use the CURLOPT_SSLCERT certificate.

CURLOPT_SSLCERTTYPE

CURLOPT_SSLENGINE

CURLOPT_SSLENGINE_DEFAULT

CURLOPT_SSLKEY

CURLOPT_SSLKEYPASSWD

CURLOPT_SSLKEYTYPE

CURLOPT_URL
You can also set the URL address in the curl_init () function of PHP.

CURLOPT_USERAGENT
The HTTP request contains a string containing the "user-agent" header.

CURLOPT_USERPWD
Pass the username and password required for a connection in the format of [username]: [password].

CURLOPT_HTTP200ALIASES
The setting no longer processes the HTTP 200 response in the form of an error. the format is an array.

CURLOPT_HTTPHEADER
Sets an array of transmitted content in a header.

CURLOPT_POSTQUOTE

CURLOPT_QUOTE

CURLOPT_FILE
Set the location of the output file. The value is a resource type. the default value is STDOUT (browser ).

CURLOPT_INFILE
The address of the file to be read when uploading the file. The value is a resource type.

CURLOPT_STDERR
Set an error output address. The value is a resource type that replaces the default STDERR.

CURLOPT_WRITEHEADER
Sets the address of the file to which the header is written. The value is a resource type.

CURLOPT_HEADERFUNCTION
Set a callback function. this function has two parameters: the first is the resource handle of curl, and the second is the output header data. The output of header data must depend on this function to return the size of written data.

CURLOPT_PASSWDFUNCTION
Set a callback function with three parameters: the first is the resource handle of curl, the second is a password prompt, and the third parameter is the maximum allowed password length. Returns the password value.

CURLOPT_READFUNCTION
Set a callback function with two parameters: the first is the resource handle of curl and the second is the data read. Data reading must depend on this function. The size of the data to be read, such as 0 or EOF.

CURLOPT_WRITEFUNCTION
Set a callback function with two parameters: the first is the resource handle of curl and the second is the written data. Data writing must depend on this function. Returns the exact size of the written data.

8. Summary of CURLINFO parameters:

CURLINFO_EFFECTIVE_URL
Last valid url address

CURLINFO_HTTP_CODE
The last received HTTP code

CURLINFO_FILETIME
The time when the document is obtained remotely. if the document cannot be obtained, the returned value is "-1"

CURLINFO_TOTAL_TIME
Time consumed by the last transmission

CURLINFO_NAMELOOKUP_TIME
Time consumed by name resolution

CURLINFO_CONNECT_TIME
Time used to establish a connection

CURLINFO_PRETRANSFER_TIME
Time used to prepare the transmission from the established connection

CURLINFO_STARTTRANSFER_TIME
The time used to start the connection.

CURLINFO_REDIRECT_TIME
The time used for redirection before the start of transaction transmission.

CURLINFO_SIZE_UPLOAD
Total uploaded data volume

CURLINFO_SIZE_DOWNLOAD
Total downloaded data volume

CURLINFO_SPEED_DOWNLOAD
Average download speed

CURLINFO_SPEED_UPLOAD
Average upload speed

CURLINFO_HEADER_SIZE
Header Size

CURLINFO_HEADER_OUT
Request string

CURLINFO_REQUEST_SIZE
The size of the problematic HTTP request

CURLINFO_SSL_VERIFYRESULT

CURLINFO_CONTENT_LENGTH_DOWNLOAD
Length of the downloaded Content read from Content-Length: field

CURLINFO_CONTENT_LENGTH_UPLOAD
Description of uploaded content size

CURLINFO_CONTENT_TYPE
The "Content-type" value of the downloaded Content. NULL indicates that the server has not sent a valid "Content-Type: header"

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.