PHP simulation post behavior code summary (POST method is not absolutely safe)_PHP tutorial

WBOY
Release: 2016-07-21 15:20:20
Original
1294 people have browsed it

There are two methods to choose from: First: handwritten code. Second: Use the HttpClient php class library
The first method:

Copy the code The code is as follows:

< ? PHP
$flag = 0;
//Data to be posted
$argv = array(
'var1'=>'abc',
'var2'=>'you Okay');
//Construct the string to be posted
foreach ($argv as $key=>$value) {
if ($flag!=0) {
$params .= "&";
$flag = 1;
}
$params.= $key."="; $params.= urlencode($value);
$flag = 1;
}
$length = strlen($params);
//Create socket connection
$fp = fsockopen("127.0.0.1",80,$errno,$errstr,10) or exit ($errstr."--->".$errno);
//Construct the header of the post request
$header = "POST /mobile/try.php HTTP/1.1";
$header .= "Host:127.0.0.1";
$header .= "Referer:/mobile/sendpost.php";
$header .= "Content-Type: application/x-www-form-urlencoded" ;
$header .= "Content-Length: ".$length."";
$header .= "Connection: Close";
//Add the post string
$header . = $params."";
//Send post data
fputs($fp,$header);
$inheader = 1;
while (!feof($fp)) {
$line = fgets($fp,1024); //Remove the header of the request packet and only display the return data of the page
if ($inheader && ($line == "n" || $line == "" )) {
$inheader = 0;
}
if ($inheader == 0) {
echo $line;
}
}
fclose($fp) ;
?>

The second method is: use the httpclient class
Copy the code The code is as follows:

$pageContents = HttpClient::quickPost('http://example.com/someForm', array(
'name' => 'Some Name',
'email' = > 'email@example.com'
));

Using the httpclient class library, you can download the latest class library from the official address: http://scripts.incutio. com/httpclient/index.php
Attach some php httpclient and several other usages
Static method to obtain web pages:
Copy code The code is as follows :

$pageContents = HttpClient::quickGet('http://bankcha.com')

 Get method gets
Copy code The code is as follows:

$client = new HttpClient('bankcha.com');
if (!$client->get('/')) {
die('An error occurred: '. $client->getError());
}
$pageContents = $client->getContent();
Get method with debugging
PHP code
$client = new HttpClient('bankcha.com');
$client->setDebug(true);
if (!$client->get('/')) {
die('An error occurred : '.$client->getError());
}
$pageContents = $client->getContent();
Get method with automatic redirection
PHP code
$ client = new HttpClient('www.bankcha.com');
$client->setDebug(true);
if (!$client->get('/')) {
die ('An error occurred: '.$client->getError());
}
$pageContents = $client->getContent();
Check if the page exists
PHP code
$client = new HttpClient('bankcha.com');
$client->setDebug(true);
if (!$client->get('/thispagedoesnotexist')) {
die('An error occurred: '.$client->getError());
}
if ($client->getStatus() == '404') {
echo 'Page does not exist!';
}
$pageContents = $client->getContent();
Fake client
PHP code
$client = new HttpClient('bankcha.com' );
$client->setDebug(true);
$client->setUserAgent('Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.3a) Gecko/20021207 ');
if (!$client->get('/')) {
die('An error occurred: '.$client->getError());
}
$pageContents = $client->getContent();
Login verification and request a web page
PHP code
$client = new HttpClient('bankcha.com');
$client-> ;post('/login.php', array(
'username' => 'Simon',
'password' => 'ducks'
));
if (!$ client->get('/private.php')) {
die('An error occurred: '.$client->getError());
}
$pageContents = $client- >getContent();
HTTP Authorization
PHP code
$client = new HttpClient('bankcha.com');
$client->setAuthorization('Username', 'Password') ;
if (!$client->get('/')) {
die('An error occurred: '.$client->getError());
}
$ pageContents = $client->getContent();
Output header information
PHP code
$client = new HttpClient('bankcha.com');
if (!$client->get ('/')) {
die('An error occurred: '.$client->getError());
}
print_r($client->getHeaders());
Set the maximum number of redirects within a domain
PHP code
$client = new HttpClient('www.bankcha.com');
$client->setDebug(true);
$client- >setMaxRedirects(3);
$client->get('/');

php fsockopen fakes post and get methods
fsockopen fakes post and get methods, if You are looking for PHP processing code that fakes post and get methods. This one is good.
Copy code The code is as follows:

//fsocket simulated post submission
$purl = "http://localhost/netphp/test2.php?uu=rrrrrrrrrr";
print_r(parse_url($url));
sock_post($purl,"uu=55555555555555555");
/ /fsocket simulates get submission
function sock_get($url, $query)
{
$info = parse_url($url);
$fp = fsockopen($info["host"], 80 , $errno, $errstr, 3);
$head = "GET ".$info['path']."?".$info["query"]." HTTP/1.0rn";
$head .= "Host: ".$info['host']."rn";
$head .= "rn";
$write = fputs($fp, $head);
while (!feof($fp))
{
$line = fread($fp,4096);
echo $line;
}
}
sock_post($purl, "uu=rrrrrrrrrrrrrr");
function sock_post($url, $query)
{
$info = parse_url($url);
$fp = fsockopen($info["host"] , 80, $errno, $errstr, 3);
$head = "POST ".$info['path']."?".$info["query"]." HTTP/1.0rn";
$head .= "Host: ".$info['host']."rn";
$head .= "Referer: http://".$info['host'].$info[ 'path']."rn";
$head .= "Content-type: application/x-www-form-urlencodedrn";
$head .= "Content-Length: ".strlen(trim( $query))."rn";
$head .= "rn";
$head .= trim($query);
$write = fputs($fp, $head);
while (!feof($fp))
{
$line = fread($fp,4096);
echo $line;
}
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325137.htmlTechArticleHere are two methods to choose from: First: handwritten code. Second: Use the HttpClient php class library. First method: Copy the code. The code is as follows: ? PHP $flag = 0; //Data to be posted $a...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template