A detailed tutorial on PHP curl

Source: Internet
Author: User
Tags http authentication http post ldap php script terminates ftp transfer ssl certificate sub domain

Curl can use the syntax of the URL to simulate a browser to transfer data,

Because it is a mock browser, it also supports multiple protocols,

Protocols such as FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE, and LDAP can all be well supported, including some:
HTTPS authentication, HTTP POST method, HTTP put method, FTP upload, keyberos authentication, HTTP upload, proxy server, cookies, username/password Authentication,

Download file breakpoint Continuation, upload file breakpoint continuation, HTTP proxy server pipeline, even it also supports IPV6,SCOKET5 proxy server, upload files via HTTP proxy server
To the FTP server, and so on.

That's why we want to use curl!
Using curl to complete a simple request is mainly divided into the following four steps:
1. Initialize, create a new curl resource
2. Set the URL and the appropriate options
3. Crawl the URL and pass it to the browser
4. Turn off the Curl resource and release the system resources
Let's take a page, usually we use the file_get_contents () function to get:
Like this:

1.<?php2. 3. $str = file_get_contents (' Http://www.shouce.ren '); 4. 5.//or: 6. 7. $str = File ("Http://www.shouce.ren"); 8. 9.//or: 10. 11.readfile ("Http://www.shouce.ren"); 12. 13.?>

In this way we will find that we have no way to handle the error effectively, and more importantly, we have no way to complete some difficult tasks:
such as: processing of cookies, verification, form submission, file upload and so on.
OK, now let's use the code to complete the four steps of curl above:

1.<?php2. 3.//1. Initialize, create a new curl Resource 4. 5. $ch = Curl_init (); 6. 7.//2. Set the URL and the corresponding option 8. 9.curl_setopt ($ch, Curlopt_url, "Http://www.shouce.ren"); 10. 11.curl_setopt ($ch, Curlopt_header, 0); 12. 13.//3. Crawl the URL and pass it to browser 14. 15.curl_exec ($ch); 16. 17.//4. Close the Curl resource and release system Resource 18. 19.curl_close ($ch); 20. 21.?>

In the four steps above, the second step is the most critical and you can set some advanced options:
For example, Curlopt_url and Curlopt_header in the previous example, which represent "URL addresses to get" and "Output header files as data streams when enabled", are just the tip of the iceberg, and we can set a number of options:

Options Optional value values Note
Curlopt_autoreferer Referer: information is automatically set in the header when based on location : redirection.
Curlopt_binarytransfer Returns the native (raw) output when curlopt_returntransfer is enabled.
Curlopt_cookiesession When enabled, Curl simply passes a session cookie, ignoring other cookies, and by default, curl returns all cookies to the server. Session cookies are cookies that are used to determine if the session on the server is valid.
Curlopt_crlf Converts a Unix newline character to a carriage return line break when enabled.
Curlopt_dns_use_global_cache When enabled, a global DNS cache is enabled, which is thread-safe and enabled by default.
Curlopt_failonerror The HTTP status code is displayed, and the default behavior is to ignore HTTP messages with numbers less than or equal to 400.
Curlopt_filetime When enabled, attempts to modify the information in the remote document. The resulting information is returned through the curlinfo_filetime option of the Curl_getinfo () function. Curl_getinfo ().
Curlopt_followlocation When enabled, the "location:" returned by the server server is returned to the server recursively in the header, using curlopt_maxredirs to limit the number of recursive returns.
Curlopt_forbid_reuse Forced disconnection after completion of the interaction, cannot be reused.
Curlopt_fresh_connect Forces a new connection to be taken in place of the connection in the cache.
Curlopt_ftp_use_eprt When enabled, when FTP is downloaded, use the EPRT (or lprt) command. When set to FALSE , disable EPRT and LPRT, using the port command only.
Curlopt_ftp_use_epsv When enabled, the EPSV command is first attempted before replying to PASV mode during FTP transfer. When set to FALSE , the EPSV command is disabled.
Curlopt_ftpappend Append writes to the file instead of overwriting it when enabled.
Curlopt_ftpascii The alias of the Curlopt_transfertext .
Curlopt_ftplistonly Only the name of the FTP directory is listed when enabled.
Curlopt_header When enabled, the information for the header file is output as a data stream.
Curlinfo_header_out The request string to trace the handle when enabled. Available starting from PHP 5.1.3. The curlinfo_ prefix is intentional (intentional).
Curlopt_httpget When enabled, the method for HTTP is set to get, because get is the default, so it is used only if it is modified.
Curlopt_httpproxytunnel When enabled, it is transmitted over an HTTP proxy.
Curlopt_mute Restores the default values for all modified parameters in the Curl function when enabled.
Curlopt_netrc After the connection is established, access the ~/.NETRC file to get the user name and password information to connect to the remote site.
Curlopt_nobody When enabled, the body portion of the HTML is not output.
Curlopt_noprogress Turns off the progress bar of the curl transfer when enabled, and the default setting for this entry is enabled. 
Note: PHP automatically sets this option to TRUE, and this option should only be changed for debugging purposes.
Curlopt_nosignal When enabled, ignores all the signals that curl passes to PHP. This entry is enabled by default when SAPI multi-threaded transmissions. When CURL 7.10 is added.
Curlopt_post When enabled, a regular post request is sent with the type:application/x-www-form-urlencoded, just as the form was submitted.
Curlopt_put Allow HTTP to send files when enabled, you must set both curlopt_infile and curlopt_infilesize.
Curlopt_returntransfer The information obtained by CURL_EXEC () is returned as a file stream, rather than as a direct output.
Curlopt_ssl_verifypeer When disabled, curl terminates the validation from the server. Use the curlopt_cainfo option to set the certificate using the curlopt_capath option to set the certificate directory if curlopt_ssl_verifypeer(the default value is 2) is enabled, Curlopt_ssl_verifyhost needs to be set to TRUE otherwise set to FALSE. The default is TRUEfrom Curl 7.10. The default binding installation starts with Curl 7.10.
Curlopt_transfertext When enabled, use ASCII mode for FTP transmissions. For LDAP, it retrieves plain text information rather than HTML. On Windows systems, the system does not set STDOUT to binary mode.
Curlopt_unrestricted_auth The user name and password information is continuously appended to multiple locations in the header generated by using curlopt_followlocation , even if the domain name has changed.
Curlopt_upload Allow file uploads when enabled.
Curlopt_verbose When enabled, all information is reported and stored in the STDERR or specified curlopt_stderr .

For the optional parameters of these options below, value should be set to an integer type:

Options Optional value values Note
Curlopt_buffersize The size of the cache is read in each fetch, but there is no guarantee that the value will be filled every time. 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 supported for the time being.
Curlopt_connecttimeout The time to wait before initiating the connection, or infinite wait if set to 0.
Curlopt_connecttimeout_ms The time, in milliseconds, to try to connect the wait. If set to 0, the wait is infinite. Added in Curl 7.16.2. Available starting from PHP 5.2.3.
Curlopt_dns_cache_timeout Sets the time to save DNS information in memory by default of 120 seconds.
Curlopt_ftpsslauth FTP authentication method:Curlftpauth_ssl (first try SSL),curlftpauth_tls (first try TLS) or curlftpauth_default ( Let curl decide automatically). Added in Curl 7.12.2.
Curlopt_http_version Curl_http_version_none (default, let CURL decide which version to use),curl_http_version_1_0 (Force http/1.0) or curl_http_version_1_1 (Force use http/1.1 )。
Curlopt_httpauth The HTTP authentication methods used, the optional values are:curlauth_basic,curlauth_digest,curlauth_gssnegotiate,Curlauth _NTLM,curlauth_any and curlauth_anysafe. You can use |  The bit field (or) operator separates multiple values, and curl lets the server select one of the best supported values. Curlauth_any equivalent to curlauth_basic | Curlauth_digest | Curlauth_gssnegotiate | CURLAUTH_NTLM. Curlauth_anysafe equivalent to curlauth_digest | Curlauth_gssnegotiate | CURLAUTH_NTLM.
Curlopt_infilesize Sets the size limit for the upload file, in bytes (byte).
Curlopt_low_speed_limit When the transfer speed is less than curlopt_low_speed_limit (bytes/sec), PHP will determine if it is too slow to cancel the transfer, depending on the curlopt_low_speed_time .
Curlopt_low_speed_time When the transfer speed is less than curlopt_low_speed_limit (bytes/sec), PHP will determine if it is too slow to cancel the transfer, depending on the curlopt_low_speed_time .
Curlopt_maxconnects The maximum number of connections allowed, which is over Curlopt_closepolicy , determines which connections should be stopped.
Curlopt_maxredirs Specify the maximum number of HTTP redirects that are used with curlopt_followlocation .
Curlopt_port Used to specify the connection port. (option available)
Curlopt_protocols The bit field of the curlproto_* refers to. If enabled, the bit-domain value qualifies libcurl for the protocols that are available for use during transmission. This will allow you to support a number of protocols when compiling libcurl, but the limit is just a subset of what is allowed in them. The default libcurl will use all of the protocols it supports. See Curlopt_redir_protocols. The 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 How HTTP proxy connections are validated. Use the bit field flags in Curlopt_httpauth to set the appropriate options. For proxy authentication only curlauth_basic and CURLAUTH_NTLM are currently supported. Added in Curl 7.10.7.
Curlopt_proxyport The port of the proxy server. The port can also be set in curlopt_proxy .
Curlopt_proxytype Not curlproxy_http (the default) is curlproxy_socks5. was added in Curl 7.10.
Curlopt_redir_protocols The bit field value in the curlproto_* . If enabled, the bit-domain value restricts the protocol that the transport thread can use when curlopt_followlocation is turned on to follow a redirect. This will enable you to restrict the transport thread to use a subset of the allowed protocols when redirecting the default Libcurl will allow all protocols except file and SCP. This and 7.19.4 pre-release versions are unconditionally followed by a number of different supported protocols. For agreement constants, refer to curlopt_protocols. Added in Curl 7.19.4.
Curlopt_resume_from Passes a byte offset (used to continue the breakpoint) when the transfer is resumed.
Curlopt_ssl_verifyhost 1 Check that there is a common name (common name) in the server SSL certificate. Note: The common name (Common name) is generally filled in with the domain name (domain) or subdomain (sub domain) where you will be applying for an SSL certificate. 2 checks if the common name exists and matches the host name provided.
Curlopt_sslversion The SSL version used (2 or 3). By default, PHP detects this value by itself, although in some cases it needs to be set manually.
Curlopt_timecondition If edited after a time specified by curlopt_timevalue , the page is returned using curl_timecond_ifmodsince , if it has not been modified, and Curlopt_ Header is true, returns a header for "304 not Modified" , Curlopt_header to False, using Curl_timecond_ Ifunmodsince, the default value is curl_timecond_ifunmodsince.
Curlopt_timeout Sets the maximum number of seconds that curl is allowed to execute.
Curlopt_timeout_ms Sets the maximum number of milliseconds that curl allows to execute. Added in Curl 7.16.2. can be used from PHP 5.2.3.
Curlopt_timevalue Sets the timestamp used by a curlopt_timecondition , using curl_timecond_ifmodsincein the default state.

For the optional parameters of these options below, value should be set to a string type:

Options Optional value values Note
Curlopt_cainfo A file name that holds 1 or more certificates used to authenticate the server. This parameter is only meaningful when used with Curlopt_ssl_verifypeer .
Curlopt_capath A directory that holds multiple CA certificates. This option is used in conjunction with Curlopt_ssl_verifypeer .
Curlopt_cookie Set the contents of the "Cookie:" section of the HTTP request. Multiple cookies are separated by semicolons, with a semicolon followed by a space (for example, "fruit=apple; Colour=red").
Curlopt_cookiefile The file name that contains the cookie data, the format of the cookie file can be in Netscape format, or just plain HTTP header information is stored in the file.
Curlopt_cookiejar The file where the cookie information is saved after the connection ends.
Curlopt_customrequest Use a custom request message instead of "GET" or "HEAD" as an HTTP request. This is for executing "DELETE" or other more covert HTTP requests. Valid values such as "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" is incorrect. 
Note: Do not use before you determine the method that the server supports for this custom request.
Curlopt_egdsocket Similar to Curlopt_random_file, except for a entropy gathering daemon socket.
Curlopt_encoding The value of "accept-encoding:" in the HTTP request header. The supported encodings are "identity","deflate" and "gzip". If an empty string "", the request header sends all supported encoding types. was added in Curl 7.10.
Curlopt_ftpport This value will be used to obtain the IP address required for the FTP "POST" instruction. The "POST" command tells the remote server to connect to the IP address we specify. This string can be a plain text IP address, host name, a network interface name (under Unix), or just a '-' to use the default IP address.
Curlopt_interface The network send interface name, which can be an interface name, an IP address, or a host name.
Curlopt_krb4level KRB4 (Kerberos 4) security level. Any of the following values are valid (from low to high):"Clear","safe","Confidential","private". If the string does not match these, "private"will be used. When this option is set to NULL , KRB4 security authentication is disabled. Currently KRB4 security authentication can only be used for FTP transmission.
Curlopt_postfields All data is sent using the "POST" action in the HTTP protocol. To send a file, precede the file name with the @ prefix and use the full path. This parameter can be passed urlencoded after the string resembles 'Para1=val1&para2=val2& Or use a field name as the key value, the field data is an array of values. If value is an array, thecontent-type header will be set to multipart/form-data.
Curlopt_proxy HTTP proxy channel.
Curlopt_proxyuserpwd A string that is used to connect to the agent's "[Username]:[password]" format.
Curlopt_random_file A file name that is used to generate an SSL random number seed.
Curlopt_range In the "X- y" form, where X and Y are options to get the range of data, in bytes. The HTTP transport thread also supports several such duplicates separated by commas such as "X-y,n-m".
Curlopt_referer The contents of "Referer:" in the HTTP request header.
Curlopt_ssl_cipher_list A list of SSL encryption algorithms. For example, Rc4-sha and TLSv1 are available encrypted lists.
Curlopt_sslcert A file name that contains the PEM format certificate.
curlopt_sslcertpasswd The password required to use the curlopt_sslcert certificate.
Curlopt_sslcerttype The type of certificate. The supported formats are "PEM" (the default), "DER" and "ENG". Added in Curl 7.9.3.
Curlopt_sslengine The cryptographic engine variable used to specify the SSL private key in Curlopt_sslkey .
Curlopt_sslengine_default A variable used for asymmetric cryptographic operations.
Curlopt_sslkey The file name that contains the SSL private key.
curlopt_sslkeypasswd The password for the SSL private key specified in Curlopt_sslkey
Note: Since this option contains sensitive password information, remember to keep this PHP script secure.
Curlopt_sslkeytype The encryption type of the private key specified in Curlopt_sslkey , supported by the key type "PEM"(the default),"DER" and "ENG".
Curlopt_url The URL address you need to get, or you can set it in the Curl_init () function.
Curlopt_useragent A string containing a "user-agent:" header in the HTTP request.
Curlopt_userpwd Pass in a connection the user name and password required in the format:"[Username]:[password]".

For the following optional parameters of option ,value should be set to an array:

For the following optional parameters of option ,value should be set to a stream resource (for example, using fopen ()):
For the following optional parameters of option ,value should be set to a callback function name:

options Optional value values
curlopt_headerfunction Set a callback function that has two parameters, the first of which is a CU The resource handle of the RL, and the second is the header data for the output. The output of the header data must rely on this function to return the size of the data that has been written.
curlopt_passwdfunction set a callback function with three parameters, the first being a resource handle for curl, and the second a password prompt, Three parameters are the maximum allowable value for the password length. Returns the value of the password.
curlopt_progressfunction set a callback function with three parameters, first a resource handle for curl, and the second a file descriptor Resource, and the third is the length. Returns the contained data.
curlopt_readfunction has two parameters for a callback function, the first is a session handle, and the second is a string of HTTP response header information. With this function, the returned data is processed on its own. The return value is the data size, in bytes. Returns 0 for the EOF signal.
curlopt_writefunction has two parameters for a callback function, the first is a session handle, and the second is a string of HTTP response header information. With this callback function, the response header information is processed on its own. The response header information is the entire string. Sets the return value to the exact length of the written string. The transport thread terminates when an error occurs.

See, as long as the second function within the set of these options to complete the corresponding function, curl function is often powerful drop, you can try Oh!

For example, we just want to input the acquired content into a file, instead of outputting it directly to the browser, we can use the Curlopt_returntransfer option!

In this way, when Curl executes, it will output the contents of the page to the file, we can complete the collection and other functions!

In terms of efficiency, curl's principle is to simulate the operation of the browser, it is more efficient than file_get_contents () four times times higher than, imagine that we

To collect a very large amount of information on the station, operating efficiency of the difference imaginable!

Not only in terms of efficiency, but also in stability, curl is much better than the file_get_contents () function.
This is the powerful curl, the following will give you a detailed description of its advanced applications, we can simulate the browser post value, or even upload!

A detailed tutorial on PHP curl

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.