"Curl in PHP" Curl detailed explanation of PHP

Source: Internet
Author: User
Tags curl http post ldap ftp transfer

These days in helping some students to deal with the problem, suddenly found that these students are using the file_get_contents () function to capture the content of the page, seemingly there is no curl concept or not particularly sensitive to this tool,

This article I will give you a detailed introduction to the curl of the simple use of the following, I'll give you a detailed introduction of curl advanced applications,

Good, nonsense not to say, we first come to know what is curl, there may be many students have not heard of this tool, I first to give you a brief introduction of what is curl:

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

Because it is an analog browser, it also supports multiple protocols,

Protocols such as FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE, and LDAP can 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 continued transmission, upload file breakpoint, HTTP proxy server pipeline, even it also supports IPV6,SCOKET5 proxy server, upload files through HTTP proxy server

To the FTP server, and so on.

That's why we're using curl.

Using curl to complete simple requests 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. Close the Curl resource and release the system resources

We're going to take a page, and normally we'll use the file_get_contents () function to get:

Like this:
<?php $str = file_get_contents (' http://bbs.lampbrother.net ');   Or: $str = File ("Http://bbs.lampbrother.net");   Or is: ReadFile ("http://bbs.lampbrother.net"); ?>
In this way, we will find that we do not have the means to deal with error effectively, and more importantly, we have no way to accomplish some difficult tasks:

such as: processing cookies, validation, form submission, file upload and so on.

OK, now we're going to use code to complete the above Curl Four steps: <?php//1. Initialize, create a new curl resource $ch = Curl_init ();   2. Set URL and corresponding option curl_setopt ($ch, Curlopt_url, "http://www.lampbrother.net/");   curl_setopt ($ch, Curlopt_header, 0);   3. Crawl the URL and pass it to the browser curl_exec ($ch);   4. Close Curl Resources and release system resources curl_close ($CH); ?>
In these four steps, the second step is most critical, and you can set some advanced options:

For example, the Curlopt_url and Curlopt_header in the previous example, representing the URL address to obtain and the output of the header file as data flow when enabled, are just the tip of the iceberg, and we can set many options:

Options Optional value value Note
Curlopt_autoreferer Automatically sets the Referer: information in the header when redirected according to location: redirect.
Curlopt_binarytransfer Returns the native (raw) output when curlopt_returntransfer is enabled.
curlopt_cookiesession When enabled, Curl will simply pass a session cookie, ignoring the other cookies, and curl will return all cookies to the server by default. A session cookie is a cookie that is used to determine whether a server-side session is valid.
Curlopt_crlf When enabled, converts a newline character from UNIX to a carriage return line feed.
Curlopt_dns_use_global_cache When enabled, a global DNS cache is enabled, which is thread-safe and is enabled by default.
Curlopt_failonerror Displays the HTTP status code, and the default behavior is to ignore HTTP information with a number less than or equal to 400.
Curlopt_filetime When enabled, an attempt is to modify information in a remote document. The resulting information is returned through the curlinfo_filetime option of the Curl_getinfo () function. Curl_getinfo ().
curlopt_followlocation When enabled, "Location:" returned by the server server is returned recursively to the server in the header, using curlopt_maxredirs to limit the number of recursive returns.
Curlopt_forbid_reuse Forced disconnect after completing the interaction, cannot be reused.
Curlopt_fresh_connect Forces a new connection to replace the connection in the cache.
curlopt_ftp_use_eprt When enabled, use the EPRT (or LPRT) command when FTP downloads. When set to FALSE , disable EPRT and LPRT and use the port command only.
CURLOPT_FTP_USE_EPSV When enabled, first try the EPSV command before reverting to PASV mode during FTP transfer. When set to FALSE , disables the EPSV command.
Curlopt_ftpappend Append writes to the file instead of overwriting it when enabled.
Curlopt_ftpascii Alias for Curlopt_transfertext .
curlopt_ftplistonly Only the name of the FTP directory is listed when enabled.
Curlopt_header When enabled, the header file's information is exported as a data stream.
Curlinfo_header_out The request string that tracks the handle when enabled. Available starting from PHP 5.1.3. The curlinfo_ prefix is intentional (intentional).
Curlopt_httpget When enabled, the method of HTTP is set to get, because get is the default, so it is only used if it is modified.
Curlopt_httpproxytunnel When enabled, it is transmitted through the 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 username and password information to connect to the remote site.
Curlopt_nobody When enabled, the body portion of HTML is not output.
curlopt_noprogress
Turn off the progress bar for curl transmissions 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 curl passed to PHP for the signal. This entry is enabled by default when SAPI a multithreaded transmission. CURL was added when he was 7.10.
Curlopt_post When enabled, a regular post request is sent, with the type: application/x-www-form-urlencoded, just like the form submission.
Curlopt_put Allows HTTP to send files when enabled, and must also 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 will terminate validation from the server side. Setting up certificates using the curlopt_cainfo option set up certificate directories using the curlopt_capath option if curlopt_ssl_verifypeer(the default is 2) is enabled, curlopt_ssl_verifyhost need to be set to TRUE otherwise set to FALSE. Starting from Curl 7.10 defaults to TRUE. Start the default binding installation from Curl 7.10.
Curlopt_transfertext Use ASCII mode for FTP transfer when enabled. For LDAP, it retrieves plain text information rather than HTML. On Windows systems, the system does not set the STDOUT to binary mode.
Curlopt_unrestricted_auth continues to append user name and password information in 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

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.