Home >Backend Development >PHP Tutorial >PHP CURL Function Library Page 1/2_PHP Tutorial
curl_close — Close a curl session
curl_copy_handle — Copy all contents and parameters of a curl connection resource
curl_errno — Return a numeric number containing error information of the current session
curl_error — Return a character containing error information of the current session String
curl_exec — Execute a curl session
curl_getinfo — Get information about a curl connection resource handle
curl_init — Initialize a curl session
curl_multi_add_handle — Add a separate curl handle resource to a curl batch session
curl_multi_close — Close a batch handle resource
curl_multi_exec — Parse a curl batch handle
curl_multi_getcontent — Return the text stream of the obtained output
curl_multi_info_read — Get the relevant transmission information of the currently parsed curl
curl_multi_init — Initialize a curl batch handle resource
curl_multi_remove_handle — Remove a handle resource in the curl batch handle resource
curl_multi_select — Get all the sockets associated with the cURL extension, which can then be "selected"
curl_setopt_array — Set session parameters for a curl in the form of an array
curl_setopt — Set session parameters for a curl
curl_version — Get curl-related version information
The role of the curl_init() function is to initialize a Curl session, the only parameter of the curl_init() function is optional and represents a URL address.
The curl_exec() function is used to execute a curl session. The only parameter is the handle returned by the curl_init() function.
The curl_close() function is used to close a curl session. The only parameter is the handle returned by the curl_init() function.
$ch = curl_init("http://www.baidu.com/");
curl_exec($ch);
curl_close($ch);
?>
The purpose of the curl_version() function is to obtain curl-related version information. The curl_version() function has a parameter, and it is unclear what it does.
print_r(curl_version())
?>
The function of curl_getinfo() function is to obtain the information of a curl connection resource handle. The curl_getinfo() function has two parameters. The first parameter is the resource handle of curl. The second parameter is the following constants:
$ch = curl_init("http://www.baidu.com/");
print_r(curl_getinfo($ch ));
?>
Optional constants include:
CURLINFO_EFFECTIVE_URL
The last valid url address
CURLINFO_HTTP_CODE
The last received HTTP Code
CURLINFO_FILETIME
The time to obtain the document remotely, if it cannot be obtained, the return value is "-1"
CURLINFO_TOTAL_TIME
The time consumed by the last transmission
CURLINFO_NAMELOOKUP_TIME
Time spent on name resolution
CURLINFO_CONNECT_TIME
Time spent establishing connection
CURLINFO_PRETRANSFER_TIME
Time spent from establishing connection to preparing for transfer
CURLINFO_STARTTRANSFER_TIME
Time elapsed from establishment of connection to start of transfer
CURLINFO_REDIRECT_TIME
Time elapsed for redirection before transaction transfer begins
CURLINFO_SIZE_UPLOAD
Upload Total value of data volume
CURLINFO_SIZE_DOWNLOAD
Total value of downloaded data volume
CURLINFO_SPEED_DOWNLOAD
Average download speed
CURLINFO_SPEED_UPLOAD
Average upload speed
CURLINFO_HEADER_SIZE
The size of the header part
CURLINFO_HEADER_OUT
The string to send the request
CURLINFO_REQUEST_SIZE
The size of the request in question in the HTTP request
CURLINFO_SSL_VERIFYRESULT
Result of SSL certification verification requested by setting CURLOPT_SSL_VERIFYPEER
CURLINFO_CONTENT_LENGTH_DOWNLOAD
Download content length read from Content-Length: field
CURLINFO_CONTENT_LENG TH_UPLOAD
Upload content Description of size
CURLINFO_CONTENT_TYPE
The "Content-type" value of the downloaded content, NULL means that the server did not send a valid "Content-Type: header"
The function of curl_setopt() function is Set session parameters for a curl. The curl_setopt_array() function is used to set session parameters for a curl in the form of an array.
$ch = curl_init();
$fp = fopen("example_homepage.txt", "w");
curl_setopt($ch, CURLOPT_FILE, $ fp);
$options = array(
CURLOPT_URL => 'http://www.baidu.com/',
CURLOPT_HEADER => false
);
curl_setopt_array($ ch, $options);
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>
The parameters that can be set are:
CURLOPT_AUTOREFERER
Automatically set the referer information in the header
CURLOPT_BINARYTRANSFER
When CURLOPT_RETURNTRANSFER is enabled, the data will be retrieved and returned
CURLOPT_COOKIESESSION
When enabled, curl will only pass one session cookie, ignore other cookies, curl will return all cookies to the server by default. Session cookies refer to cookies that are used to determine whether the server-side session is valid.
CURLOPT_CRLF
When enabled, convert Unix line feed characters into carriage return and line feed characters.
CURLOPT_DNS_USE_GLOBAL_CACHE
When enabled, a global DNS cache is enabled. This item is thread-safe and defaults to true.
CURLOPT_FAILONERROR
Displays the HTTP status code. The default behavior is to ignore HTTP information with a number less than or equal to 400.
CURLOPT_FILETIME
When enabled, it will try to modify the information in the remote document. The result information will be returned through the CURLINFO_FILETIME option of the curl_getinfo() function.
CURLOPT_FOLLOWLOCATION
When enabled, the "Location:" returned by the server will be placed in the header and returned to the server recursively. Use CURLOPT_MAXREDIRS to limit the number of recursive returns.
CURLOPT_FORBID_REUSE
Forcibly disconnect after completing the interaction and cannot be reused.
CURLOPT_FRESH_CONNECT
Force a new connection to replace the one in the cache.
CURLOPT_FTP_USE_EPRT
TRUE to use EPRT (and LPRT) when doing active FTP downloads. Use FALSE to disable EPRT and LPRT and use PORT only.
Added in PHP 5.0.0.
CURLOPT_FTP_USE_EPSV
TRUE to first try an EPSV command for FTP transfers before reverting back to PASV. Set to FALSE to disable EPSV.
CURLOPT_FTPAPPEND
TRUE to append to the remote file instead of overwriting it .
CURLOPT_FTPASCII
An alias of CURLOPT_TRANSFERTEXT. Use that instead.
CURLOPT_FTPLISTONLY
TRUE to only list the names of an FTP directory. >When enabled, the header file information will be output as a data stream.
CURLOPT_HTTPGET
When enabled, the HTTP method will be set to GET. Because GET is the default, it is only used when it is modified.
CURLOPT_HTTPPROXYTUNNEL
When enabled, it will be transmitted through HTTP proxy.
CURLOPT_MUTE
Restore all modified parameters in the curl function to their default values.
CURLOPT_NETRC
After the connection is established, access the ~/.netrc file to obtain the username and password information to connect to the remote site.
CURLOPT_NOBODY
When enabled, the body part of the HTML will not be output.
CURLOPT_NOPROGRESS
When enabled, turn off the progress bar of curl transmission. The default setting of this item is true
CURLOPT_NOSIGNAL
When enabled, ignore all signals passed by curl to PHP. This item is turned on by default during SAPI multi-thread transmission.
CURLOPT_POST
When enabled, a regular POST request will be sent of type: application/x-www-form-urlencoded, just like a form submission.
CURLOPT_PUT
When enabled, HTTP is allowed to send files. CURLOPT_INFILE and CURLOPT_INFILESIZE must be set at the same time.
CURLOPT_RETURNTRANSFER
The information obtained by curl_exec() is returned in the form of a file stream instead of directly output.
CURLOPT_SSL_VERIFYPEER
FALSE to stop cURL from verifying the peer's certificate. Alternate certificates to verify against can be specified with the CURLOPT_CAINFO option or a certificate directory can be specified with the CURLOPT_CAPATH option. CURLOPT_SSL_VERIFYHOST may also need to be TRUE or FALSE if CURLOPT_SSL_VERIFYPEER is disabled (it defaults to 2). TRUE by default as of cURL 7.10. Default bundle installed as of cURL 7.10.
CURLOPT_TRANSFERTEXT
TRUE to use ASCII mode for FTP transfers. For LDAP, it retrieves data in plain text instead of HTML. On Windows systems, it will not set STDOUT to binary mode.
CURLOPT_UNRESTRICTED_AUTH
Continuously append users to multiple locations in the header generated using CURLOPT_FOLLOWLOCATION name and password information, even if the domain name has changed.
CURLOPT_UPLOAD
Allow file transfer when enabled
CURLOPT_VERBOSE
When enabled, all information will be reported and stored in STDERR or the specified CURLOPT_STDERR
CURLOPT_BUFFERSIZE
The size of the cache is read each time the data is obtained, and this value will be filled every time.
CURLOPT_CLOSEPOLICY
It is either CURLCLOSEPOLICY_LEAST_RECENTLY_USED or CURLCLOSEPOLICY_OLDEST. There are three others, but curl does not support them yet. .
CURLOPT_CONNECTTIMEOUT
The time to wait before initiating a connection. If set to 0, there will be no waiting.