POST data to a page using PHP
The code is as follows |
Copy Code |
<?php function Socketpost ($url, $data, & $ret) { $URLARR = Parse_url ($url); $host = $URLARR [' Host ']; $port = Isset ($urlArr [' Port ']) $URLARR [' Port ']:80; $path = Isset ($urlArr [' path ']) $URLARR [' Path ']: "/"; $fp = Fsockopen ($host, $port, $errno, $errstr, 30); if (! $fp) { echo "$errstr ($errno) <br/>n"; return false; } Else { $out = "POST $path http/1.1rn"; $out. = "Host: $hostrn"; $out. = "CONTENT-TYPE:APPLICATION/X-WWW-FORM-URLENCODEDRN"; $out. = "Content-length:". strlen ($data). RN "; $out. = "Connection:keep-alivernrn"; $out. = $data; $ret = ""; Fwrite ($fp, $out); while (!feof ($FP)) { $ret. = Fgets ($fp, 128); } Fclose ($FP); } return true; } ?> |
If the post is an error, turn the $out. = "Connection:keep-alivernrn";
Using PHP's socket to simulate an instance of sending post data
The code is as follows |
Copy Code |
<?php $domain = "127.0.0.1"; $port = 80; $uri = "/ly/post.php"; $data = "txtname=111&txtemail=222@1.net&rabsex=%d0%a1%bd%e3&txtfrom=%bd%ad%ce%f7%c1%fa%c4%cf& txtqq=2222&txturl=33333333&txtface=images%2fface%2fface05.gif&txtem=images%2fem%2fem01.gif& Txtbody=rrr "; $protocolstr = "POST {$uri} http/1.1rnhost: {$domain}rncontent-type:application/ X-www-form-urlencodedrncontent-length: ". Strlen ($data). "Rnreferer:http://10.10.10.10/ly/index.phprnuser-agent:mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1. NET CLR 1.1.4322) rnaccept: */*rnrn{$data}rnrn ";
$sock = Fsockopen ($domain, $port, $errno, $errstr, 30); if (! $sock) Die ("$errstr ($errno) n"); Fputs ($sock, $PROTOCOLSTR);
$headers = ""; while ($str = Trim (fgets ($sock, 4096)) $headers. = "$STRN";
$body = ""; while (!feof ($sock)) $body. = Fgets ($sock, 4096); Fclose ($sock);
echo "Echo $headers; echo "n";
echo "Echo $body; ?> |
Here we do not talk about the use of Fsockopen fwrite these functions, only to describe the process of imitation, there is a need to refer to the next.