PHP analog Post Behavior Code summary (post is not absolutely secure) _php tutorial

Source: Internet
Author: User
Tags fread
There are two ways to choose from: First: Handwritten code. Second: Using the HttpClient PHP class library
The first method:
Copy CodeThe code is as follows:
<? Php
$flag = 0;
The data to post
$ARGV = Array (
' var1 ' = ' abc ',
' var2 ' = ' How are you '?
Constructing a 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, ten) or exit ($errstr. " ---> ". $errno);
Constructs 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 string for post
$header. = $params. "";
Send data for Post
Fputs ($fp, $header);
$inheader = 1;
while (!feof ($fp)) {
$line = fgets ($fp, 1024); Removal of 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 method is: Use the HttpClient class
Copy CodeThe 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 go to the official download the latest class library, the official address is: http://scripts.incutio.com/httpclient/index.php
Add some points php httpclient several other uses
static methods get Web pages:
Copy CodeThe code is as follows:
$pageContents = Httpclient::quickget (' http://bankcha.com ')

Get method gets
Copy CodeThe code is as follows:
$client = new HttpClient (' bankcha.com ');
if (! $client->get ('/')) {
Die (' An error occurred: '. $client->geterror ());
}
$pageContents = $client->getcontent ();
Get method with Debug get
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 auto-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 ();
Forge 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 ();
Sign in to verify 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 in a domain
PHP code
$client = new HttpClient (' www.bankcha.com ');
$client->setdebug (TRUE);
$client->setmaxredirects (3);
$client->get ('/');

PHP Fsockopen Forge Post and Get methods
Fsockopen Forge Post and Get methods Oh, if you're looking for a fake post and get method of PHP processing code this is nice.
Copy CodeThe code is as follows:
Fsocket Analog Post Submission
$purl = "Http://localhost/netphp/test2.php?uu=rrrrrrrrrrrr";
Print_r (Parse_url ($url));
Sock_post ($purl, "uu=55555555555555555");
Fsocket Analog 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;
}
}
?>

http://www.bkjia.com/PHPjc/325137.html www.bkjia.com true http://www.bkjia.com/PHPjc/325137.html techarticle There are two ways to choose from: First: Handwritten code. Second: Using HttpClient PHP Class Library The first method: Copy code code as follows:? PHP $flag = 0; Data to post $a ...

  • 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.