Brief Introduction to PHP CURL

Source: Internet
Author: User
Tags http cookie

What is CURL? PHP and CURL?

(Php curl)

1. What is CURL?

Curl is a File Transfer tool that uses the URL syntax to work in the command line mode. Curl is a File Transfer tool that uses the URL syntax to work in the command line mode.

It supports many protocols: FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE, and LDAP. Curl also supports HTTPS authentication, http post, http put, FTP upload, kerberos authentication, HTTP upload, proxy server, cookies, user name/password authentication, and resumable download of downloaded files,

Resumable upload of uploaded files, the http proxy Server pipeline (proxy tunneling), and even IPv6 and socks5 proxy servers, which upload files to the FTP server through the http proxy server, are very powerful.

Php does not extend this function by default, but it still does not take effect. Open the PHP installation directory and search for the following three files: ssleay32.dll, libeay32.dll, and php_curl.dll,

Copy them one by one to the system32 folder in the system directory, modify the php. ini file, find the; extension = php_curl.dll line, remove the previous; number, save, restart the server

Website collection, login, COOKIE, etc.

Ii. CURL library in PHP

Curl_close-close a curl session

Curl_copy_handle-copy all content and parameters of a curl connection Resource

Curl_errno-A number containing the current session error message is returned.

Curl_error-returns a string containing the current session error message.

Curl_exec-execute a curl session

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 resource to the curl batch processing session

Curl_multi_close-close a batch processing handle Resource

Curl_multi_exec-parse a curl batch handle

Curl_multi_getcontent-return the obtained output text stream

Curl_multi_info_read-obtains the transmission information of the currently resolved curl.

Curl_multi_init-initialize a curl batch processing handle Resource

Curl_multi_remove_handle-remove a handle resource from the curl batch processing handle.

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-obtain curl-related version information

The function curl_init () initializes a curl session. The unique parameter of the curl_init () function is optional, indicating a url address.

The role of the curl_exec () function is to execute a curl session. The unique parameter is the handle returned by the curl_init () function.

The function curl_close () is used to close a curl session. The only parameter is the handle returned by the curl_init () function.

3. settings of the CURL_SETOPT () function

Bool curl_setopt (int ch, string option, mixed value)

The curl_setopt () function sets options for a CURL session. The option parameter is the setting you want, and the value is the value given by this option.

The values of the following options will be used as long integer (specified in the option parameter ):

* CURLOPT_INFILESIZE: When you upload a file to a remote site, this option tells PHP the size of the file to be uploaded.

* CURLOPT_VERBOSE: If you want CURL to report every unexpected event, set this option to a non-zero value.

* CURLOPT_HEADER: If you want to include a header in the output, set this option to a non-zero value.

* CURLOPT_NOPROGRESS: if you do not display a process entry for CURL transmission in PHP, set this option to a non-zero value. Note: PHP automatically sets this option to a non-zero value. You should change this option only for debugging purposes.

* CURLOPT_NOBODY: if you do not want to include the body in the output, set this option to a non-zero value.

* CURLOPT_FAILONERROR: If you want PHP not to be displayed when an error occurs (HTTP code returns a value greater than or equal to 300), set this option to a non-zero value. By default, a normal page is returned, ignoring the code.

* CURLOPT_UPLOAD: If you want PHP to prepare for upload, set this option to a non-zero value.

* CURLOPT_POST: If you want PHP to create a regular http post, set this option to a non-zero value. This POST is a common application/x-www-from-urlencoded type, most of which are used by HTML forms.

* CURLOPT_FTPLISTONLY: set this option to a non-zero value. PHP will list the FTP directory names.

* CURLOPT_FTPAPPEND: set this option to a non-zero value. PHP overwrites the Remote Application file.

* CURLOPT_NETRC: set this option to a non-zero value. PHP will be in your ~. In the/netrc file, find the username and password of the remote site you want to establish a connection.

* CURLOPT_FOLLOWLOCATION: set this option to a non-zero value (like "Location:") header. The server will send it as part of the HTTP header (note that this is recursive, PHP will send the header like "Location ).

* CURLOPT_PUT: sets this option to upload a file over HTTP as a non-zero value. To upload this file, you must set the CURLOPT_INFILE and CURLOPT_INFILESIZE options.

* CURLOPT_MUTE: set this option to a non-zero value. PHP will be completely silenced for the CURL function.

* CURLOPT_TIMEOUT: specifies the maximum number of seconds for a long integer.

* CURLOPT_LOW_SPEED_LIMIT: sets the number of long integers to control the number of bytes transmitted.

* CURLOPT_LOW_SPEED_TIME: sets the number of long integers and controls the number of seconds to transmit the number of bytes specified by CURLOPT_LOW_SPEED_LIMIT.

* CURLOPT_RESUME_FROM: transmits a long integer parameter containing the byte offset address (the start form you want to transfer ).

* CURLOPT_SSLVERSION: transmits a long parameter containing the SSL version. By default, PHP will be determined by its own efforts. You must set it manually in more security scenarios.

* CURLOPT_TIMECONDITION: transmits a long parameter to specify how to process the CURLOPT_TIMEVALUE parameter. You can set this parameter to TIMECOND_IFMODSINCE or TIMECOND_ISUNMODSINCE. This is only used for HTTP.

* CURLOPT_TIMEVALUE: the number of seconds from January 1, to the present. This time will be used by the CURLOPT_TIMEVALUE option as the specified value, or by the default TIMECOND_IFMODSINCE.

The values of the following options will be used as strings:

* CURLOPT_URL: the URL you want to retrieve with PHP. You can also set this option when initializing with the curl_init () function.

* CURLOPT_USERPWD: transmits a string in the format of [username]: [password] to connect to PHP.

* CURLOPT_PROXYUSERPWD: transmits a string in the format of [username]: [password] to connect to the HTTP proxy.

* CURLOPT_RANGE: transmits a range you want to specify. It should be in the "X-Y" format, X or Y is excluded. HTTP shipping also supports several intervals separated by sentences (X-Y, N-M ).

* CURLOPT_POSTFIELDS: transmits a string of all data for the HTTP "POST" operation.

* CURLOPT_REFERER: a string containing the "referer" header in an HTTP request.

* CURLOPT_USERAGENT: a string containing the "user-agent" header in an HTTP request.

* CURLOPT_FTPPORT: transmits an IP address that contains the IP address used by the ftp "POST" command. This POST Command tells the remote server to connect to the specified IP address. This string can be an IP address, a host name, a network interface Name (under UNIX), or '-' (using the default IP address of the system ).

* CURLOPT_COOKIE: transmits a header connection containing the HTTP cookie.

* CURLOPT_SSLCERT: transmits a string containing the PEM format certificate.

* CURLOPT_SSLCERTPASSWD: pass a password that includes the password required to use the CURLOPT_SSLCERT certificate.

* CURLOPT_COOKIEFILE: a string that transmits the name of a file containing cookie data. This cookie file can be in the Netscape format or heap containing the HTTP header in the file.

* CURLOPT_CUSTOMREQUEST: When an HTTP request is sent, a character is used by GET or HEAD. It is helpful to perform DELETE or other operations. Pass a string to be used instead of GET or HEAD when doing an HTTP request. this is useful for doing or another, more obscure, HTTP request. note: Do not do this before confirming that your server supports commands. The following options require a file description (obtained by using the fopen () function ):

* CURLOPT_FILE: This file will be the output file you placed for transfer. The default value is STDOUT.

* CURLOPT_INFILE: this file is the input file you sent.

* CURLOPT_WRITEHEADER: This file contains the header of your output.

* CURLOPT_STDERR: this file is written incorrectly, not stderr. This example is used to obtain the page to be logged on. The current practice is to log on every time or every time, and the users who need it have made improvements.

4. Examples

Example 1
View plaincopy to clipboardprint?

$ Cookie_jar = tempnam (./tmp, cookie );

$ Ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, http ://******);

Curl_setopt ($ ch, CURLOPT_POST, 1 );

$ Request = email_address = & password = & action =;

Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ request );
// Save the returned cookie information in the $ cookie_jar file.

Curl_setopt ($ ch, CURLOPT_COOKIEJAR, $ cookie_jar );

// Set whether the returned data is automatically displayed

Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );

// Set whether to display the header information

Curl_setopt ($ ch, CURLOPT_HEADER, false );

// Set whether to output the page content

Curl_setopt ($ ch, CURLOPT_NOBODY, false );

Curl_exec ($ ch );

Curl_close ($ ch); // get data after login

$ Ch2 = curl_init ();

Curl_setopt ($ ch2, CURLOPT_URL, http ://*****);

Curl_setopt ($ ch2

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.