PHP and curl

Source: Internet
Author: User
Tags http cookie set cookie

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
$ Cookie_jar = tempnam ('./tmp', 'cooker ');

$ 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, curlopt_header, false );

Curl_setopt ($ CH2, curlopt_returntransfer, 1 );

Curl_setopt ($ CH2, curlopt_cookiefile, $ cookie_jar );

$ Orders = curl_exec ($ CH2 );

Echo '';

Echo strip_tags ($ orders );

Echo '';

Curl_close ($ CH2 );

2,

Set_time_limit (0 );
// Set the execution time of the program to no limit
USD I = 10000;
// Perform the game 10000 times
For ($ J = 0; $ J & lt; $ I; $ J ++)
{

$ Url = "http://www.yoururl.com/bbs/savepost.asp ";
// This address is the URL of the action in the reply form.
$ CH = curl_init ();

Curl_setopt ($ ch, curlopt_url, $ URL );

Curl_setopt ($ ch, curlopt_referer, "http://www.hxfoods.com/bbs/dispbbs.asp? Boardid = 14 & amp; replyid = 672709 & amp; id = 127437 & amp; page = 1 & amp; Skin = 0 & amp; star = 53 ");
// Set the source address. If the source address is not set, the Forum server may not be allowed to reply for verification.
Curl_setopt ($ ch, curlopt_cookiesession, true );
// Save the cookie
Curl_setopt ($ ch, curlopt_cookie, "dvforum = userid = 24122 & amp; usercookies = 0 & amp; userhidden = 2 & amp; Password = w0reu0000775vry745 & amp; userclass = % 96% 7C & amp; username = % a1 % fa % a1 % fa % B8 % D2 % CB % C0 % B6 % D3 % a1 % fa & amp; statuserid = 2194783945 ");
// Set the cookie here.
Curl_setopt ($ ch, curlopt_post, 1 );
Curl_setopt ($ ch, curlopt_nobody, 1 );
// The content is not displayed, because many forums need to jump automatically after replying.
Curl_setopt ($ ch, curlopt_postfields, 'body = Response & amp; followup = 672709 & amp; rootid = 127437 & amp; star = 58 & amp; totalusetable = dv_bbs3 & amp; username = → death squad → & amp; signflag = 1 & amp; Total = 65535 ');
// Assign values to the parameters of the reply form you analyzed.
Curl_setopt ($ ch, curlopt_header, true );
Curl_exec ($ ch );
If (curl_errno ($ ch ))
{
Print curl_error ($ ch );
}
Else
{
Curl_close ($ ch );
}

}

Summary:
Steps:
1. During the entire operation, the first step is to use the cur_init () function for initialization.
$ Curl = curl_init ('www .example.com ');
2. Use the curl_setopt () function to set the options.
3. execute the transaction after setting
Curl_exec ($ curl );
4. Close curl_close ();

Common
1 curl_setopt ($ curl, curlopt_failonerror, 1); stops curl when an error occurs.
2 curl_setopt ($ curl, curlopt_followlocation, 1); Allow curl redirection
3 curl_setopt ($ curl, curlopt_post, 1); Use the POST method for Curl
4 curl_setopt ($ curl, curlopt_timout, 5); set the supertime
5 curl_setopt ($ curl, curlopt_postfields, "$ data"); Set Post Data

Another small example:
Define ("cookiejar", tempnam (ini_get ("upload_tmp_dir"), "cookie "));
$ Request = curl_init ();
Curl_setopt ($ request, curlopt_url, 'HTTP: // 127.0.0.1/form. php ');
Curl_setopt ($ request, curlopt_post, 1 );
Curl_setopt ($ request, curlopt_postfields, "SB = XX & name = pp"); // how to transmit values in post Mode
Curl_setopt ($ request, curlopt_returntransfer, true );
Curl_setopt ($ request, curlopt_cookiejar, cookiejar); // set cookie
Curl_setopt ($ request, curlopt_cookiefile, cookiejar );
$ Content = curl_exec ($ request );
Echo $ content;

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.