Basic curl tutorial

Source: Internet
Author: User
Tags http cookie
Simply put, there are four steps in total: curl_init (); curl_setopt (); curl_exec (); curl_close (); first, try whether your curl is installed correctly & lt ;? Phpprint_r (curl_version ());? & Gt; display Array (

Simply put, there are four steps in total.

Curl_init ();

Curl_setopt ();

Curl_exec ();

Curl_close ();

First try whether your curl is installed correctly.

Display back if correct

Array ([version_number] => 462848 [age] => 2 [features] => 540 [ssl_version_number] => 0 [version] => 7.16.0 [host] => i386-pc-win32 [ssl_version] => OpenSSL/0.9.8e [libz_version] => 1.2.3 [protocols] => Array ([0] => tftp [1] => ftp [2] => telnet [3] => dict [4] => ldap [5] => http [6] => file [7] => https [8] => ftps ))

These are version information...

Then try the four steps above.

Curl_setopt ($ ch, CURLOPT_URL, "http: // 127.0.0.1 /");

Curl_setopt ($ ch, CURLOPT_HEADER, 0 );

Curl_exec ($ ch );

Curl_close ($ ch);?>

You should be able to see the http: // 127.0.0.1 content... suppose your 127.0.0.1 has content...

This seems useless. Otherwise, you can save it with ob or your own commands. this is an absolutely omnipotent download... it is also the basis for omnipotent collection !!!

$ Ch = curl_init ();

// CURLOPT_URL is the address of the content to be obtained

// CURLOPT_HEADER is the obtained header fr

// CURLOPT_PORT is the port

// CURLOPT_RETURNTRANSFER stores the output content in multi_getcontent.

$ Options = array (CURLOPT_URL => 'http: // localhost /',

CURLOPT_HEADER => 0,

CURLOPT_NOBODY => 0,

CURLOPT_PORT => 80,

CURLOPT_RETURNTRANSFER => 1

);

Curl_setopt_array ($ ch, $ options );

Curl_exec ($ ch );

Echo curl_multi_getcontent ($ ch );

// Print_r (curl_getinfo ($ ch ));

Curl_close ($ ch );

 

// Without using multi_getcontent, you can also operate with ob...

// Ob_start ("callback ");

// Echo "the output of the page ";

// Ob_end_flush ();

// Function callback ($ buffer)

//{

//???? $ Buffer. = "--- by ipractitioner ";

//???? File_put_contents('test.txt ', $ buffer );

//???? Return "saved successfully ";

//}

?>

Functions:

Curl_init () Initialization

$ Ch = curl_init ()

Curl_setopt () is the most important and the content is the most.

What do the two in the demo mean?

CURLOPT_URL is the address of the content to be obtained.

CURLOPT_HEADER: whether to output the HEADER

Curl_setopt_array () is a supplement to curl_setopt, used to replace two curl_setopt ()

$ Options = array (CURLOPT_URL => 'http: // localhost /',

CURLOPT_HEADER => false

);

Curl_setopt_array ($ ch, $ options );

The above section is equal

Curl_setopt ($ ch, CURLOPT_URL, "http: // localhost /");

Curl_setopt ($ ch, CURLOPT_HEADER, 0 );

Some people may say this is useless. you can see that multi is useless.

In fact, curl is to write more parameters and perform more operations. Therefore, it must be written in batches to facilitate modification.

Curl_exec ($ ch) execution

Curl_getinfo ($ ch, $ opt) does not actually need $ opt. The result is the operation status, which is used after exec.

Curl_close ($ ch) closes a connection

Curl_copy_handle ($ ch) copies a $ ch

Curl_version () to obtain the current version information-print_r (curl_version ());

Curl_errno ($ ch) returns the last error number. When can I report it...

Curl_error ($ ch) returns the last error message. When can I report this...

Curl_multi_getcontent () is multi, but it is put here because multi_init () is not required for this object. its function is to get the result of exec () without Output. a string is returned,

For example:

Curl_setopt ($ scheme, CURLOPT_RETURNTRANSFER, true );

Exec(‑ch‑policfile_put_contents('test.txt ', curl_multi_getcontent ($ ch ));

In this way, we can implement post-processing of the obtained data, rather than the output. of course, we can also use ob series functions to control the data.

The following are the multi items...

Curl_multi_init ()

Curl_multi_add_handle ()

Curl_multi_exec ()

Curl_multi_remove_handle ()

Curl_multi_close ()

The preceding five processes can be used as follows:

// Create both cURL resources
$ Response = curl_init ();
$ Ch2 = curl_init ();
// Set URL and other appropriate options
Curl_setopt ($ scheme, CURLOPT_URL, "http://www.example.com /");
Curl_setopt ($ scheme, CURLOPT_HEADER, 0 );
Curl_setopt ($ ch2, CURLOPT_URL, "http://www.php.net /");
Curl_setopt ($ ch2, CURLOPT_HEADER, 0 );
// Create the multiple cURL handle
$ Mh = curl_multi_init ();
// Add the two handles
Curl_multi_add_handle ($ mh, $ handle );
Curl_multi_add_handle ($ mh, $ ch2 );
$ Running = null;
// Execute the handles
Do {
Curl_multi_exec ($ mh, $ running );
} While ($ running> 0 );
// Close the handles
Curl_multi_remove_handle ($ handle );
Curl_multi_remove_handle ($ ch2 );
Curl_multi_close ($ mh );
?>

In fact, it is to use multiple processes to run two common curl_exec ()

 

Curl_multi_select (): returns all sockets available for $ mh. I didn't understand this yet, and I didn't understand it...

Curl_multi_info_read () returns the operation status, which is similar to getinfo.

 

The following is a Chinese description of the opt parameter comparison:

Ps: This section in the php document is in English. it's hard to understand !!!!!!

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, and PHP will completely silence 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 to the page once every time. if you need it, you can make improvements :)

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.