PHP simulated post Behavior Code summary (post is not absolutely safe) _php tips

Source: Internet
Author: User
Tags fread php class strlen trim
Here are two ways to choose: First: Handwriting code. Second: Using HttpClient PHP class Library
The first method:
Copy Code code as follows:

? Php
$flag = 0;
The data to post
$ARGV = Array (
' var1 ' => ' abc ',
' Var2 ' => ' How are you? '
Construct the string to post
foreach ($argv as $key => $value) {
if ($flag!=0) {
$params. = "&";
$flag = 1;
}
$params. = $key. " ="; $params. = UrlEncode ($value);
$flag = 1;
}
$length = strlen ($params);
Create a socket connection
$fp = Fsockopen ("127.0.0.1", $errno, $errstr,) or exit ($ERRSTR.) ---> ". $errno);
To construct the header of a 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 a Post string
$header. = $params. "";
Send the Post data
Fputs ($fp, $header);
$inheader = 1;
while (!feof ($fp)) {
$line = fgets ($fp, 1024); Removing the header of the request package shows only the return data of the page
if ($inheader && ($line = = "N" | | $line = = "")) {
$inheader = 0;
}
if ($inheader = = 0) {
Echo $line;
}
}
Fclose ($FP);
?>

The second approach is to: Use the HttpClient class
Copy Code code as follows:

$pageContents = Httpclient::quickpost (' Http://example.com/someForm ', Array (
' Name ' => ' Some name ',
' Email ' => ' email@example.com '
));

Use the HttpClient class library, you can go to the official download the latest class library, the official address is: http://scripts.incutio.com/httpclient/index.php
Add a few other uses of PHP httpclient
static method to get a Web page:
Copy Code code as follows:

$pageContents = Httpclient::quickget (' http://bankcha.com ')

Get method gets
Copy Code code as follows:

$client = new HttpClient (' bankcha.com ');
if (! $client->get ('/')) {
Die (' An error occurred: '. $client->geterror ());
}
$pageContents = $client->getcontent ();
Get method with Debug gets
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 steering
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 ();
Logon authentication 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 ());
Sets the maximum number of redirects in a domain
PHP code
$client = new HttpClient (' www.bankcha.com ');
$client->setdebug (TRUE);
$client->setmaxredirects (3);
$client->get ('/');

PHP Fsockopen forged Post and Get methods
Fsockopen forged Post and get methods well, if you're looking for a PHP code that forges the post and getting methods, this is good.
Copy Code code as follows:

<?php
Fsocket Analog Post Submission
$purl = "Http://localhost/netphp/test2.php?uu=rrrrrrrrrrrr";
Print_r (Parse_url ($url));
Sock_post ($purl, "uu=55555555555555555");
Fsocket Simulate Get commit
function Sock_get ($url, $query)
{
$info = Parse_url ($url);
$fp = Fsockopen ($info ["host"], $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=rrrrrrrrrrrrrrrr");
function Sock_post ($url, $query)
{
$info = Parse_url ($url);
$fp = Fsockopen ($info ["host"], $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;
}
}
?>

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.