Two Methods for simulating POST data submission in php

Source: Internet
Author: User
Tags unix domain socket

The following summarizes two methods in php that can simulate user login or post data implementation, which is very useful to everyone. If you need it, please refer to it.

Curl Function
CURL Library Function 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.

Example

The Code is as follows: Copy code

$ Post_data = array ();
$ Post_data ['clientname'] = "test08 ";
$ Post_data ['clientpasswd'] = "test08 ";
$ Post_data ['submit '] = "submit ";
$ Url = 'HTTP: // xxx. xx/xxx/top. php ';
$ O = "";
Foreach ($ post_data as $ k => $ v)
{
$ O. = "$ k =". urlencode ($ v )."&";
}
$ Post_data = substr ($ o, 0,-1 );
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_POST, 1 );
Curl_setopt ($ ch, CURLOPT_HEADER, 0 );
Curl_setopt ($ ch, CURLOPT_URL, $ url );
// To support cookies
Curl_setopt ($ ch, CURLOPT_COOKIEJAR, 'cookie.txt ');
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_data );
$ Result = curl_exec ($ ch );

Simulate User Logon
Simulate logon to sina

We need to capture data, which may be post-login content. At this time, we need to use the simulated login function of curl.

 

The Code is as follows: Copy code

& Lt ;? Php

Function checklogin ($ user, $ password)
{
If (empty ($ user) | empty ($ password ))
{
Return 0;
}
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_REFERER, "http://mail.sina.com.cn/index.html ");
Curl_setopt ($ ch, CURLOPT_HEADER, true );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
Curl_setopt ($ ch, CURLOPT_USERAGENT, USERAGENT );
Curl_setopt ($ ch, CURLOPT_COOKIEJAR, COOKIEJAR );
Curl_setopt ($ ch, CURLOPT_TIMEOUT, TIMEOUT );
Curl_setopt ($ ch, CURLOPT_URL, "http://mail.sina.com.cn/cgi-bin/login.cgi ");
Curl_setopt ($ ch, CURLOPT_POST, true );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, "& amp; logintype = uid & amp; u =". urlencode ($ user). "& amp; psw =". $ password );
$ Contents = curl_exec ($ ch );
Curl_close ($ ch );
If (! Preg_match ("/Location: (. *)/cgi/index. php? Check_time = (. *) n/", $ contents, $ matches ))
{
Return 0;
} Else {
Return 1;
}
}

& Nbsp; define ("USERAGENT", $ _ SERVER ['HTTP _ USER_AGENT ']);
Define ("COOKIEJAR", tempnam ("/tmp", "cookie "));
Define ("TIMEOUT", 500 );

& Nbsp; echo checklogin ("zhangying215", "xtaj227 ");
? & Gt;

2. Use fsockopen


Description of the. PHP fsockopen function:

Open Internet or Unix domain socket connection (Open socket link)

Initiates a socket connection to the resource specified by target.

Fsockopen () returns a file pointer which may be used together with the other file functions (such as fgets (), fgetss (), fwrite (), fclose (), and feof ()). returns a file handle.

Enable the PHP fsockopen Function

PHP fsockopen must be enabled by the allow_url_fopen option in PHP. ini.

The Code is as follows: Copy code


$ URL = 'HTTP: // xxx. xx/xxx/top. php ';
$ Post_data ['clientname'] = "test08 ";
$ Post_data ['clientpasswd'] = "test08 ";
$ Post_data ['submit '] = "too many temporary connections ";
$ Referrer = "";
// Parsing the given URL
$ URL_Info = parse_url ($ URL );
// Building referrer
If ($ referrer = "") // if not given use this script as referrer
$ Referrer = $ _ SERVER ["SCRIPT_URI"];
 
// Making string from $ data
Foreach ($ post_data as $ key => $ value)
$ Values [] = "$ key =". urlencode ($ value );
 
$ Data_string = implode ("&", $ values );
// Find out which port is needed-if not given use standard (= 80)
If (! Isset ($ URL_Info ["port"])
$ URL_Info ["port"] = 80;
// Building POST-request:
$ Request. = "POST". $ URL_Info ["path"]. "HTTP/1.1n ";
$ Request. = "Host:". $ URL_Info ["host"]. "n ";
$ Request. = "Referer: $ referrern ";
$ Request. = "Content-type: application/x-www-form-urlencodedn ";
$ Request. = "Content-length:". strlen ($ data_string). "n ";
$ Request. = "Connection: closen ";
$ Request. = "n ";
$ Request. = $ data_string. "n ";
$ Fp = fsockopen ($ URL_Info ["host"], $ URL_Info ["port"]);
Fputs ($ fp, $ request );
While (! Feof ($ fp )){
$ Result. = fgets ($ fp, 128 );
}
Fclose ($ fp );

If

Warning: fsockopen () has been disabled for security reasons in D :... Cos-html-cachecos-html-cache.php on line 35
Cos-html-cache of other versions is not supported. Then find the following method. (The result is invalid because all functions are disabled .)
We have tried to replace functions with other methods.
1. How to disable fsockopen ()
The following are two common methods to disable fsockopen.
1. Modify php. ini and add disable_functions = to fsockopen.
2. Modify php. ini and change allow_url_fopen = On to allow_url_fopen = Off.
Ii. how to disable the fsockopen Function
1. If the server does not disable pfsockopen at the same time, replace the fsockopen function with pfsockopen.
Specific Operation: search for the string fsockopen in the Program (replace with pfsockopen (. Example:
Before modification:
$ Fp = fsockopen ($ host, 80, $ errno, $ errstr, 30 );
After modification:
$ Fp = pfsockopen ($ host, 80, $ errno, $ errstr, 30 );


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.