PHP analog User Send post data method

Source: Internet
Author: User

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.

Related Article

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.