Home  >  Article  >  Backend Development  >  PHP CURL function library_PHP tutorial

PHP CURL function library_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:35:29733browse

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 for the current session
curl_error — return an error message containing String of current session error message
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 to curl batch session Individual curl handle resources
curl_multi_close — Close a batch handle resource
curl_multi_exec — Parse a curl batch handle
curl_multi_getcontent — Return a text stream of the fetched output
curl_multi_info_read — Get the currently parsed curl Related transmission information
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
curl_init() function Function 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 curl_version() function is used to obtain curl-related version information. The curl_version() function has a parameter, but 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, and the second parameter is as follows Some 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 when the document was retrieved remotely, if If it cannot be obtained, the return value is "-1"
CURLINFO_TOTAL_TIME
The time consumed by the last transmission
CURLINFO_NAMELOOKUP_TIME
The time consumed by name resolution
CURLINFO_CONNECT_TIME
The time consumed by establishing the connection Time
CURLINFO_PRETRANSFER_TIME
The time it takes from establishing the connection to preparing the transfer
CURLINFO_STARTTRANSFER_TIME
The time it takes from establishing the connection to the start of the transfer
CURLINFO_REDIRECT_TIME
Redirect everything before the transaction transfer starts Time used
CURLINFO_SIZE_UPLOAD
Total value of uploaded data
CURLINFO_SIZE_DOWNLOAD
Total value of downloaded data
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_LENGTH_UPLOAD
Description of upload content size
CURLINFO_CONTENT_TYPE
"Content of download content - type" value, NULL means that the server did not send a valid "Content-Type: header"
The function of curl_setopt() function is to 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 settable parameters are:
CURLOPT_AUTOREFERER
Automatically set the referer information in the header
CURLOPT_BINARYTRANSFER
When CURLOPT_RETURNTRANSFER is enabled, data will be obtained and returned
CURLOPT_COOKIESESSION
When enabled, curl will only pass one session cookie and ignore other cookies. By default, curl will All cookies are returned to the server. 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 disconnects 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.
CURLOPT_HEADER
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 in 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 being output directly.
CURLOPT_SSL_VERIFYPEER
FALSE to stop cURL from verifying the peers 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 username and password information to multiple locations in the header generated using CURLOPT_FOLLOWLOCATION, 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
Read in the data obtained each time The size of the cache, 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.

CURLOPT_DNS_CACHE_TIMEOUT
Set the time to save DNS information in memory, the default is 120 seconds.
CURLOPT_FTPSSLAUTH
The FTP authentication method (when is activated): CURLFTPAUTH_SSL (try SSL first), CURLFTPAUTH_TLS (try TLS first), or CURLFTPAUTH_DEFAULT (let cURL decide).
CURLOPT_HTTP_VERSION
Set curl to use HTTP protocol, CURL_HTTP_VERSION_NONE (let curl decide for itself), CURL_HTTP_VERSION_1_0 (HTTP/1.0), CURL_HTTP_VERSION_1_1 (HTTP/1.1)
CURLOPT_HTTPAUTH
HTTP verification method used, optional values ​​are: CURLAUTH_BASIC, CURLAUTH_DIGEST, CURLAUTH_GSSNEGO TIATE,CURLAUTH_NTLM , CURLAUTH_ANY, CURLAUTH_ANYSAFE, you can use the "|" operator to separate multiple values, curl allows the server to choose a value that supports the best, CURLAUTH_ANY is equivalent to CURLAUTH_BASIC | CURLAUTH_DIGEST | CURLAUTH_GSSNEGOTIATE | CURLAUTH_NTLM, CURLAUTH_ANYSAFE is equivalent to CURLAUTH_DIGEST | CURLAUTH_GSSNEGO TIATE | CURLAUTH_NTLM
CURLOPT_INFILESIZE
Set the upload file size
CURLOPT_LOW_SPEED_LIMIT
When uploading

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508328.htmlTechArticlecurl_close — Close a curl session curl_copy_handle — Copy all contents and parameters of a curl connection resource curl_errno — Return a file containing the current The numeric number of the session error message...

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn