Phpcurl_setopt and simulated user logon-PHP source code

Source: Internet
Author: User
Tags http cookie
Ec (2); the php tutorial curl_setopt and the simulated User Login boolcurl_setopt (intch, stringoption, mixedvalue) curl_setopt () function will set options for a curl session. The option parameter is the setting you want, and the value is the value given by this option. This example uses simulated logon & nbsp; $ cookie_filetempna script ec (2); script

Php tutorial curl_setopt and simulated User Logon

Bool curl_setopt (int ch, string option, mixed value)
 
The curl_setopt () function sets options for a curl session. The option parameter is the one you want to set,

Value is the value given by this option.

// The example is simulated logon.
$ Cookie_file = tempnam ('./temp', 'cookie '); // Save the temporary cookie
$ Login_url = 'HTTP: // xxx/login. php'; // address submitted on the logon page
$ Post_fields = 'username = & password = '; // value passing Parameter

$ Ch = curl_init ($ login_url );
Curl_setopt ($ ch, curlopt_header, 0 );
Curl_setopt ($ ch, curlopt_returntransfer, 1 );
Curl_setopt ($ ch, curlopt_post, 1 );
Curl_setopt ($ ch, curlopt_postfields, $ post_fields );
Curl_setopt ($ ch, curlopt_cookiejar, $ cookie_file );
Curl_exec ($ ch );
Curl_close ($ ch );


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 to upload the file

The size of the parts.
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.

Is 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 to encounter an error (http code returns a value greater than or equal to 300 ),

, Not displayed. Set this option to a non-zero one. 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 html

Form usage.
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 file of the application.

.
Curlopt_netrc: set this option to a non-zero value. php will be in your ~. /Search in the netrc file

The user name and password of the remote site to be connected.
Curlopt_followlocation: set this option to a non-zero value (like 'location: ') header,

The server will send it as a part of the http header (note that this is recursive, php will send as shown in

'Location: 'header ).
Curlopt_put: set this option to a non-zero value to upload a file over http. Upload this document

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 for transmission.

The number of bytes specified by curlopt_low_speed_limit.
Curlopt_resume_from: transmits a long integer parameter that contains the byte offset address.

Start form ).
Curlopt_sslversion: transmits a long parameter containing the ssl version. By default, php will be tried by itself

Are you sure you want to set more security manually.
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

The curlopt_timevalue option is used as the specified value or used by 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 use the curl_init () function to initialize

Set this option.
Curlopt_userpwd: transmits a string in the format of [username]: [password], which is used by php.

Connection.
Curlopt_proxyuserpwd: transmits a string in the format of [username]: [password] to be connected.

Connect to the http proxy.
Curlopt_range: transmits a range you want to specify. It should be in 'x-y' format, except that x or y is

. Http transmission also supports several intervals separated by funny sentences (x-y, n-m ).
Curlopt_postfields: a string that transmits all data for the http "post" operation.
Curlopt_referer: a string containing the 'Referer' header in an http request.
Curlopt_useragent: contains a string with the 'user-agent' header in the http request.
Curlopt_ftpport: transmits an IP address that contains the IP address used by the ftp 'post' command. This post command

Tell the remote server to connect to the specified IP address. This string can be an IP address,

Host Name, a network interface name (in unix), or '-' (use 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 contains 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 files can be in the netscape format or heap files with http headers.
Curlopt_customrequest: When an http request is sent, a character is used by get or head. Is

It is helpful to perform delete or other operations, and 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.

Function curl_redir_exec ($ ch, $ debug = "")
{
Static $ curl_loops tutorial = 0;
Static $ curl_max_loops = 20;

If ($ curl_loops ++ >=$ curl_max_loops)
{
$ Curl_loops = 0;
Return false;
}
Curl_setopt ($ ch, curlopt_header, true );
Curl_setopt ($ ch, curlopt_returntransfer, true );
$ Data = curl_exec ($ ch );
$ Debbbb = $ data;
List ($ header, $ data) = explode ("nn", $ data, 2 );
$ Http_code = curl_getinfo ($ ch, curlinfo_http_code );

If ($ http_code = 301 | $ http_code = 302 ){
$ Matches = array ();
Preg_match ('/location :(.*?) N/', $ header, $ matches );
$ Url = @ parse_url (trim (array_pop ($ matches )));
// Print_r ($ url );
If (! $ Url)
{
// Couldn't process the url to redirect
$ Curl_loops = 0;
Return $ data;
}
$ Last_url = parse_url (curl_getinfo ($ ch,

Curlinfo_inclutive_url ));
/* If (! $ Url ['scheme '])
$ Url ['scheme '] = $ last_url ['scheme'];
If (! $ Url ['host'])
$ Url ['host'] = $ last_url ['host'];
If (! $ Url ['path'])
$ Url ['path'] = $ last_url ['path']; */
$ New_url = $ url ['scheme '].': // '. $ url ['host']. $ url ['path']

. ($ Url ['query']? '? '. $ Url ['query']: '');
Curl_setopt ($ ch, curlopt_url, $ new_url );
// Debug ('redirecting to ', $ new_url );

Return curl_redir_exec ($ ch );
} Else {
$ Curl_loops = 0;
Return $ debbbb;
}
}
?>

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.